Friday, May 9, 2014

Make private method public in ruby




class Foo

  private

  def my_method
    'it work !'
  end
end
Foo.new.my_method
# => NoMethodError: private method `my_method' called for #<Foo:0x00000003ddb8e8>

Foo.send :public, :my_method
Foo.new.my_method
# => "it work !" 

class Bar < Foo
  public :my_method
end
Foo.new.my_method
# => NoMethodError: private method `my_method' called for #<Foo:0x00000003ddb8e8>

Bar.new.my_method
# => "it work !" 
source