Ruby refinements not working in CI server - ruby

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 :-)

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.

Identify troubling spec with rspec in Ruby

I've got this monstrous error message while running rspec spec/vector_spec.rb and I couldn't identify what spec is causing an error.
Here's some part of the error message:
..................F...........******************************...........F...***********************....*.....*....................................../home/ubuntu/workspace/daru/lib/daru/index.rb:102: [BUG] Segmentation fault at 0x007fe1e31ce030
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0048 p:0021 s:0197 e:000196 METHOD /home/ubuntu/workspace/daru/lib/daru/index.rb:102
c:0047 p:0015 s:0193 e:000192 METHOD /home/ubuntu/workspace/daru/lib/daru/vector.rb:207
...
Here's the full error message: https://gist.github.com/lokeshh/83369d71ca94b07cf89b23e215214666
I want to know which spec is causing an error so I can look into it but I couldn't find a way to identify the spec that is causing the error.
Is there way?
You can append a colon and then the line number of the specific test to the RSpec file, e.g.:
rspec spec/vector_spec.rb:83
You could also disable large parts of your code with =begin and =end to narrow down the section.
I just ran rspec -h and got some very helpful information. Check out the --only-failures and next-failure options! Also, looks like you could also use -fd to get the groups and example names printed out!

Ruby Uninitialized constant error when gem is run outside of bundle exec

I have a simple gem that creates an MD5 from a string.
module SimpleMD5
require 'digest/md5'
def self.md5_string(string)
Digest::MD5.hexdigest(string)
end
end
Running bundle exec bin/console and calling the method works fine
require 'simple_md5'
SimpleMD5.md5_string('test')
=> 098f6bcd4621d373cade4e832627b4f6
Once the gem is built using rake build and rake install using the IRB console and the same example above I get an error.
NameError: uninitialized constant SimpleMD5::Digest
Am I missing a step when the gem is built?
You're missing the module definition, so use this pattern:
require 'digest/md5'
module SimpleMD5
def md5_string(string)
Digest::MD5.hexdigest(string)
end
extend self
end
The SimpleMD5 name is not generated automatically, you must declare it somewhere.
Here extend self means you can mix-in the module with include SimpleMD5 as well as just use it straight-up as you do in your example.
Don't forget two things: In Ruby return is implicit, there's no need to use that unless you're exiting your function early, and MD5 is a pretty terrible hashing algorithm for 2016. Unless you're using it for backwards compatibility, use something better like SHA256 or SHA512.

Migrating to rubinius

I'm trying to migrate my project from mri to rubinius to get concurrency advantage.
I've started the server and open first page and then got error:
Puma caught this error: undefined method `=~' for Pathname (NameError)
kernel/common/module.rb:212:in `instance_method'
kernel/common/module.rb:354:in `undef_method'
kernel/bootstrap/array.rb:66:in `each'
kernel/common/module.rb:352:in `undef_method'
...
My Gemfile
source 'https://rubygems.org'
ruby '2.1.0', :engine => "rbx", engine_version: '2.2.1'
gem "rubysl" # Ruby Standard Library meta-gem for rubinius
# Server requirements
gem 'puma'
...
What might be a problem here?
UPDATE: full stack trace
I examined your stack trace and looked at the Rubinius source code. The offending line is:
class Pathname
undef =~ # THIS IS IT
end
#=~ is an instance method on Object so normally undef =~ should work on any class... unless it has been undef'd on Object or on Pathname already.
I'm wondering if this is happening because you have the rubysl gem in your Gemfile. I don't know Rubinius, but from what I can see, it doesn't seem to require you to specifically include this gem. Or maybe it did in past versions, but doesn't now. If the standard library is being loaded twice, that would explain why undef =~ fails the second time.
If that doesn't help, I recommend you try temporarily removing as many gems as possible and see if the problem disappears. If so, add them back one by one until you find which one is causing the problem.

Ruby can't find the method capture2e from open3 module

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.

Resources