Reputation: 42192
I need to run multiple methods from an array like the folowing example, first approach works but i'd rather let just run the methods without displaying the result. How to adapt the second approach so that i don't get the error below ?
def method1
print 1
end
def method2
print 2
end
[method1, method2].each(&p) #=>12 (works)
[method1, method2].each(&method(:run))
#=>12 `method': undefined method `run' for class `Object' (NameError)
Upvotes: 0
Views: 164
Reputation: 46667
Because method1
invokes the method (rather than referring to it), your arrays actually contain the results of running the methods, not references to the methods themselves.
You probably want:
[:method1, :method2].each {|m| method(m).call}
Upvotes: 2