From: "David A. Black" Date: 2009-02-09T01:38:15+09:00 Subject: Re: calling random Hi -- On Mon, 9 Feb 2009, Boris Schmid wrote: > Apologies, I have been out of using ruby for a while, and ran into > trouble when I tried to do the below. What is the normal way to call > apply a list of arguments to a list of functions? > > def f1(n) > puts "f1: #{n}" > end > > def f2(n) > puts "f2: #{n}" > end > > irb(main):008:0> [f1,f2].each {|fun| [1,2,3,4].each {|val| fun(val)}} If f1 and f2 contain actual method objects, you can use call: f1,f2 = method(:f1), method(:f2) [f1,f2].each {|m| [1,2,3,4].each {|e| m.call(e) } } Mixing f(unction) and m(ethod) terminology a bit... but that's OK, because it will also work if f1 and f2 are Proc objects (which are basically anonymous functions. If you have strings or symbols, you can use them to get the method objects (see above), or you can use send: ["f1", "f2"].each {|m| [1,2,3,4].each {|e| send(m,e)} } David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!