Object#define_singelton_method in Ruby 2.1.0 defines private methods?

96 Views Asked by At
object = Object.new
object.define_singleton_method(:foo) do
  :bar
end
object.foo

fails with private method `foo' called for #<Object:0x00000001e89580> (NoMethodError). Ruby 2.0.0 does not behave like that. Is this a bug in 2.1.0 or intentional change?

2

There are 2 best solutions below

0
On BEST ANSWER

It's a known issue. The bug was reported at #9005 and #9141.

0
On

Since define_singleton_method (and the other define_method methods) now return a symbol of the method name, you can fix this by adding public in front of any call to define_method.

public define_method(:foo) { :bar }