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..
Written by Jack Weiss
Related protips
1 Response
For now also - jack.method(:say).()
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#