25 May 2009

How to call a private method without using send

In Ruby one can invoke a private or protected method by calling send. However in ruby 1.9 send cannot be used to invoke private/protected method. No need to alarm. In stead of send use send! in ruby 1.9.

However I started thinking about what are the other ways of calling private method. I came up with three solutions.

class Car
  
  private

  def honk
    puts 'beep beep'
  end

end


c = Car.new

c.send(:honk)

c.method(:honk).call

c.instance_eval { honk }

class << c
  public :honk
end
c.honk
class << c
  private :honk
end

If anyone knows of any other solution then please post it in comments.