[Ruby Trick] Retrieve an object's private method without calling its `method`

github logo ・1 min read

Goal: Get the bar method

class Foo
  def method; end


  def bar

foo = Foo.new

# foo.method(:bar) will cause error cause it's been overridden
# foo.public_method(:bar) won't work cause :bar is private

method_method = Object.method(:method).unbind
bar_method = method_method.bind(foo).call(:bar)
bar_method.call #=> "hey!"
twitter logo DISCUSS
Classic DEV Post from May 7 '19

The newly announced future of .NET - unifying all the things

Microsoft announces .NET 5

Stan Lo profile image

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️