Hey all,
Kind of an idle, syntax comparison question here…
In python, you can apparently pass method references around very
easily. So imagine you have these 2 functions defined:
def firstWay(arg1, arg2)
return ‘Tastes great.’
def secondWay(arg1, arg2)
return ‘Less filling.’
Then you can define a method that takes a method name as an argument,
and calls it just by throwing parens (and any expected arguments of
course) after it.
def doStuff(whichway, first_arg, second_arg)
return whichway(first_arg, second_arg)
So calling:
puts doStuff(firstWay, None, None)
Would result in ‘Tastes great.’
What’s the most graceful way to do this sort of thing in ruby? I
tried passing in e.g., firstWay.to_proc, but that got me a complaint
about not having enough arguments on the call. I can imagine doing,
e.g.,
first_pointer = Proc.new do |foo, bar|
return firstWay(foo, bar)
end
One for each alternate method & passing one of those in. I can also
imagine having doStuff take a block that would call the desired
function & yielding out to that. But neither of those are as pretty
as the python I think. Are those my best options in ruby, or is there
another way?
Thanks!
-Roy