From: Fritz Trapper on 30 Dec 2009 06:42 I want to pass a reference to function as parameter to another function and execute the passed function. Somthing like this: def executer(func) func(1) end def test(x) p x end executer(test) How to write this correctly in ruby? -- Posted via http://www.ruby-forum.com/.
From: Marvin Gülker on 30 Dec 2009 06:48 Fritz Trapper wrote: > I want to pass a reference to function as parameter to another function > and execute the passed function. Somthing like this: > > def executer(func) > func(1) > end > > def test(x) > p x > end > > executer(test) > > How to write this correctly in ruby? Are you looking for the #method method? ---------------------------------------- irb(main):001:0> def a irb(main):002:1> puts "hello" irb(main):003:1> end => nil irb(main):004:0> def b(f) irb(main):005:1> f.call irb(main):006:1> end => nil irb(main):007:0> m = method(:a) => #<Method: Object#a> irb(main):008:0> b(m) hello => nil irb(main):009:0> ------------------------------------------ Marvin -- Posted via http://www.ruby-forum.com/.
From: Jesús Gabriel y Galán on 30 Dec 2009 06:51 On Wed, Dec 30, 2009 at 12:42 PM, Fritz Trapper <ajfrenzel(a)web.de> wrote: > I want to pass a reference to function as parameter to another function > and execute the passed function. Somthing like this: > > def executer(func) > func(1) > end > > def test(x) > p x > end > > executer(test) > > How to write this correctly in ruby? A typical way would be using blocks or procs: def executer yield 1 end executer {|x| puts "I got #{x}"} If you want something to handle around in a variable: func = lambda {|x| puts "I got #{x}"} executer(&func) # => I got 1 Or if in the executer you want to store the block for later use: def executer(&block) @save_for_later = block end #later... @save_for_later.call(1) #or @save_for_later[1] Hope this helps, Jesus.
From: Jeff Peng on 30 Dec 2009 07:00 Jes�s Gabriel y Gal�n: > On Wed, Dec 30, 2009 at 12:42 PM, Fritz Trapper <ajfrenzel(a)web.de> wrote: >> I want to pass a reference to function as parameter to another function >> and execute the passed function. Somthing like this: >> >> def executer(func) >> func(1) >> end >> >> def test(x) >> p x >> end >> >> executer(test) >> >> How to write this correctly in ruby? > > A typical way would be using blocks or procs: > > def executer > yield 1 > end > > executer {|x| puts "I got #{x}"} > > If you want something to handle around in a variable: > > func = lambda {|x| puts "I got #{x}"} > executer(&func) # => I got 1 > for &func, what's the "&" before "func" here? Thanks.
From: Fritz Trapper on 30 Dec 2009 07:08 Thanks for your quick replies. I see, my scenario is somewhat more complicated, than I wrote in my initial posting. The point is, that I want to pass an object and a method. Something like this: def executer(obj, method) f.method(1) end class x def test(x) p x end end o = x.new executer(o, test) -- Posted via http://www.ruby-forum.com/.
|
Next
|
Last
Pages: 1 2 3 Prev: How to avoid \ escaping when using "= <<-EOF" ? Next: Why can't I use "or" here? |