Last Updated: May 28, 2017
·
5.913K
· jackattack

Different ways to call a method in Ruby

I was recently asked to think of all the ways you can call a method in Ruby.

class Person
  def say
    'hello!'
  end
end

jack = Person.new

There's the obvious way:

jack.say

You can send the method name:

jack.send(:say)
jack.public_send(:say)

Maybe you want to grab the method, and then call it like a proc:

jack.method(:say).call

Or you can get weird with it using instance_eval. Like, really weird:

jack.instance_eval { say }
jack.instance_eval { instance_eval { say } }
jack.instance_eval { instance_eval { instance_eval { say } } }
# etc..

1 Response
Add your response

For now also - jack.method(:say).()

over 1 year ago ·