Ruby can't find the method capture2e from open3 module - ruby

I am trying to use the script blogger.rb and I just can't get it work. It keeps giving me the error :
blogger.rb:294:in text2html': undefined methodcapture2' for Open3:Module (NoMethodError)
The script does a require Open3 in the beginning. I don't understand where is the problem ! I have no knowledge of Ruby. However, I can intelligently read and edit codes in general.

I'd guess that you're using Ruby 1.8 but the script requires 1.9. The Open3 class in 1.8.7 has a popen3 class method and nothing else. The Ruby 1.9 Open3 has the capture2 and capture2e class methods that you're looking for. So you need to upgrade your Ruby to 1.9 or find another script.

Related

Ruby 2.4.1 Dir.children( dirname ) returns "undefined method `children' for Dir:Class"

I'm new to Ruby and trying to learn it. I'm using the latest Ruby version (2.4.1) and the interactive Ruby Shell.
I've come across the children method in the Dir class. I've tried the example from the documentation:
Dir.children("testdir") #=> ["config.h", "main.rb"]
but it doesn't seem to work, because I get the following message:
undefined method `children' for Dir:Class
What am I missing?
This seems to be some kind of documentation mess.
The Dir.children method was introduced with Feature #11302 into Ruby and was committed to trunk and eventually released with Ruby 2.5.0. However, it appears that the patch adding this method was not actually backported to Ruby 2.4 since dir.c of Ruby 2.4.1 doesn't mention the method. It's not immediately clear why the documentation for this method turned up at http://ruby-doc.org/
In any case, it appears you are yet out of luck with this method. You can however use the following equivalent code with your Ruby version:
Dir.entries('testdir') - [".", ".."]
It will return the exact same values as Dir.children('testdir') would in Ruby 2.5 and newer.

Compiled or obfuscated Ruby

I have a set of files that seem to be obfuscated or compiled by Ruby. If I do a file [sic] to one of the files:
a /usr/bin/env ruby script text executable
all of them start with this:
#!/usr/bin/env ruby
require 'iseq';RubyVM::InstructionSequence.load(Marshal.load(File.read(__FILE__,nil,113))).eval
What is this file? How can I see the code or debug it?
NOTE: Ruby version ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]
This is code compiled to a Ruby Virtual Machine. It is using the iseq gem which exposes the private method RubyVM::InstructionSequence::load.
You can't extract the original source code from it, but the debugger should work. You can read the compiled code in something like a human readable form with RubyVM::InstructionSequence#disassemble. Assuming Marshal.load returns a RubyVM::InstructionSequence object, this should do it.
require 'iseq';puts RubyVM::InstructionSequence.load(Marshal.load(File.read(__FILE__,nil,161))).disass‌​emble

Ruby Prime Class Issues

So I am running Ruby 1.9.3 and I am trying to use the Prime class.
I have added require mathn at the top of my .rb file
Other than the method name, this code came from the documentation page:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/prime/rdoc/Prime.html
def prime_number(n)
Prime.each(n) do |prime|
p prime
end
end
prime_number(100)
Any ideas why this doesn't work? The error I get says
undefined method each for Prime:Class
I tried it in Ruby 1.9.3 and it worked. Tried it again in Ruby 1.8.7 and got your error message. A newer version of Ruby will probably solve your problem.
Your problem is that you did require mathn and you must have require 'mathn' at the top of your file.
Either that, or you have neglected the quotes when you described your code above.

Ruby refinements not working in CI server

I'm having errors within the Jenkins server:
$ ruby -v
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
When running rspec, I have the following error:
undefined method `using' for #<Class:0x000000026f9c88> (NoMethodError)
the exact same code works on my local computer, with ruby2.
Here's my version: ruby 2.0.0dev (2012-12-01 trunk 38126) [x86_64-linux]
Also, it works on irb. It seems that ruby isn't recognizing the using statement when running a script.
Here's the code:
describe "blah" do
include TestHelper
using TestHelper::BrowserRefinement
...
end
clarification: the refinement is defined in a different file. I'm scourging the interwebs to see if there's a difference between revisions r39474 and r38126.
In current release of Ruby 2.0 (2.0.0p0), using is an instance method of the top-level object main, not that of Module. And it's a private method. If you call it in class/module definition or method definition, a RuntimeError is raised.
"The scope of a refinement activated by main.using is from the point just after main.using is invoked to the end of the file where main.using is invoked. However, when main.using is invoked in a string given as the first argument of Kernel#eval, Kernel#instance_eval, or Module#module_eval, the end of the scope is the end of the string."
You can read more about this in Refinements Specification.
For your test cases, you can write them with eval and pass in the top level bindings, like the test cases in ruby source.
Refinements is still an experimental feature, it may change in future :-)

Ruby install jcode

I'm trying to get 'jcode' for ruby, but I type "gem install jcode" and it says nothing exists?
Does anyone know why? I'm trying to manipulate UTF-8 encoded strings.
Ruby >= 1.9 doesn't require jcode, a module to handle japanese (EUC/SJIS) strings, as it supports unicode natively.
This should fix the problem:
require 'jcode' if RUBY_VERSION < '1.9'
but must say I did not test it deeply...
Have you tried simply:
require 'jcode'
?
As it happens, jcode is part of the Ruby Standard Library.
Try not to include it at all. It should be included automatically in later versions of Ruby.

Resources