The following example was showed by my professor in class and it worked perfectly fine and printed
def printv(g)
puts g.call("Fred")
puts g.call("Amber")
end
printv(method(:hello))
>>hello Fred
hello Amber
but when I am trying to run it on my irb/RubyMine its showing undefined method error. I am trying the exact code what he showed in class. What am I missing?
Thanks!
If you look at the code for
printv, you’ll see thatgwill have to provide acallmethod. There are several classes in Ruby that provide acallmethod by default, amongst them procs and lambdas:Here
hellois a variable storing a lambda, so you don’t need a symbol (:hello) to reference it.Now let’s look at the method
method. According to the docs it “[l]ooks up the named method as a receiver in obj, returning a Method object (or raising NameError)”. It’s signature is “obj.method(sym) → method”, meaning it takes a symbol argument and returns a method object. If you callmethod(:hello)now, you’ll get theNameErrormentioned in the docs, since there is currently no method named “hello”. As soon as you define one, things will work though:This also explains why the call
printv(method("hello")that you mention in your comment on the other answer fails:methodtries to extract a method object, but fails if there is no method by that name (a string as argument seems to work by the way, seems likemethodinterns its argument just in case).