Ruby 2.0's refinements aren't so refined... yet?
I began fiddling with the cool new features in Ruby 2.0.0-rc2 and quickly ran into some strangeness:
# refine_test.rb
module Doubler
refine Fixnum do
def double
self * 2
end
end
end
using Doubler
class Thing
def initialize
p 2.double
end
end
# using Doubler #=> putting this here would result in
# line 10 producing a NoMethodError for `2.double`
Thing.new # => 4
p 2.double # => 4
load 'refine_load_file_test.rb'
And the other file:
# refine_load_file_test.rb
Thing.new # => 4
p 2.double # => NoMethodError
The load
command runs Thing.new
and returns a 4
just peachy… however the next line fails. Ruby-doc doesn’t give any clues as to why this would work like that.