How to pass a function as parameter?

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/.

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/\.

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.

···

On Wed, Dec 30, 2009 at 12:42 PM, Fritz Trapper <ajfrenzel@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?

Jesús Gabriel y Galán:

···

On Wed, Dec 30, 2009 at 12:42 PM, Fritz Trapper <ajfrenzel@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.

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/.

Correction:

def executer(obj, method)
   f.method(1)
end

Should be:
def executer(obj, method)
   obj.method(1)
end

···

--
Posted via http://www.ruby-forum.com/\.

Fritz Trapper wrote:

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)

Why not just use o.send(:test) ?

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

A method can receive regular parameters and a "special" block
parameter. The & lets you pass a proc as that special block parameter,
instead of a regular one:

irb(main):033:0> executer(func)
ArgumentError: wrong number of arguments (1 for 0)
  from (irb):33:in `executer'
  from (irb):33
  from :0

Jesus.

···

On Wed, Dec 30, 2009 at 1:00 PM, Jeff Peng <jeffpeng@netzero.net> wrote:

Jesús Gabriel y Galán:

On Wed, Dec 30, 2009 at 12:42 PM, Fritz Trapper <ajfrenzel@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?

def executer(obj, method)
  obj.send method , 1
end

class X
  def test(x)
     p x
  end
end

o = X.new
executer( o , :test )

···

On Wed, Dec 30, 2009 at 6:08 AM, Fritz Trapper <ajfrenzel@web.de> wrote:

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/\.

Marnen Laibow-Koser wrote:

Why not just use o.send(:test) ?

Thanks, that helps.

···

--
Posted via http://www.ruby-forum.com/\.

Correction:

def executer(obj, method)
f.method(1)
end

Should be:
def executer(obj, method)
obj.method(1)

obj.send(method, 1)

end

executer(O.new, :the_method)

Jesus.

···

On Wed, Dec 30, 2009 at 1:08 PM, Fritz Trapper <ajfrenzel@web.de> wrote:

Thanks for your help. I got it and it works fine.

Another question:
Is there some ruby reference on the web, something like
http://api.rubyonrails.org/ for example?

···

--
Posted via http://www.ruby-forum.com/.

Marnen Laibow-Koser wrote:

Fritz Trapper wrote:

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)

Then you can use instance_method(), which is like method() except it
returns an *unbound* method:

  def executer(obj, unbound_method)
    unbound_method.bind(obj).call(1234)
  end

  class X
    def test(x)
      p x
    end
  end

  o1 = X.new
  o2 = X.new
  o3 = X.new
  method = X.instance_method(:test)

  executer(o1, method)
  executer(o2, method)
  executer(o3, method)

···

--
Posted via http://www.ruby-forum.com/\.

Jesus.

···

On Wed, Dec 30, 2009 at 1:31 PM, Fritz Trapper <ajfrenzel@web.de> wrote:

Thanks for your help. I got it and it works fine.

Another question:
Is there some ruby reference on the web, something like
http://api.rubyonrails.org/ for example?

Jesús Gabriel y Galán wrote:

http://ruby-doc.org/

Thanks a lot.

···

--
Posted via http://www.ruby-forum.com/\.