From: "Peña, Botp" Date: 2007-08-17T17:26:44+09:00 Subject: Re: Passing two functions as parameters On Behalf Of Frank Meyer: # I know only one way: # # call_fncs( Proc.new { my_fnc1 }, Proc.new { my_fnc2 } ) # # and in call_fncs: # # function1.call # function2.call # # But then I can just use a block and pass as first param a flag which # indicates which function should be called. This would be not # so clean, but I could avoid the two block inside the parameter list and # have one after the function call. ok, let me take a second shot (and my apology to my previous post). i find when doing thing like this in ruby, you should think about blocks, block, it's blocks everywhere. blocks allows one to think (in a cleaner) way between being oo-oriented or fxn-oriented. when in ruby, i think pass object, when in javascript, i think pass fxns (yes, yes, i drunk too much javascript last month; hey is passing fxns in javascipt really addictive? :) anyway, this is another simple example. hopefully, this time i am correct :) irb(main):001:0> def x irb(main):002:1> puts "x" irb(main):003:1> "x" irb(main):004:1> end => nil irb(main):005:0> def y irb(main):006:1> puts "y" irb(main):007:1> "y" irb(main):008:1> end => nil irb(main):009:0> irb(main):010:0* def test(p1=lambda{x},p2=lambda{y},p3=p1) irb(main):011:1> p1.call + p2.call + p3.call irb(main):012:1> end => nil irb(main):013:0> test x y x => "xyx" hmmm, no surprises there. but you're right, it isn't so clean... so let's introduce blocks,... irb(main):014:0> def test2 irb(main):015:1> yield irb(main):016:1> end => nil irb(main):018:0> test2 {x} x => "x" irb(main):019:0> test2 {y} y => "y" hmmm, not bad. I didn't declare any lambda or proc in there.. very clean.. it's as if yield is doing something additional job for me (and i like it) .. now, let's see if we can so some ala fxnal styles using blocks,... irb(main):035:0* def test3 p1=:x irb(main):036:1> if p1 == :x irb(main):037:2> test2 {x} irb(main):038:2> else irb(main):039:2* test2 {y} irb(main):040:2> end irb(main):041:1> end => nil irb(main):042:0> test3 x => "x" irb(main):043:0> test3 :y y => "y" hmmm, not bad... but can we skip the test2 call so we can be "more" fxnal, so to speak ? ... ok irb(main):044:0> def test4 p1=:x irb(main):045:1> if p1 == :x irb(main):046:2> yield lambda{x} irb(main):047:2> else irb(main):048:2* yield lambda{y} irb(main):049:2> end irb(main):050:1> end => nil irb(main):051:0> test4 {|a| a.call} x => "x" irb(main):052:0> test4(:y) {|a| a.call} y => "y" irb(main):053:0> Is that ok? kind regards -botp