How to pass a function as parameter?

F

Fritz Trapper

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?
 
M

Marvin Gülker

Fritz said:
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>
 
J

Jesús Gabriel y Galán

I want to pass a reference to function as parameter to another function
and execute the passed function. Somthing like this:

def executer(func)
=A0 func(1)
end

def test(x)
=A0 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 =3D lambda {|x| puts "I got #{x}"}
executer(&func) # =3D> I got 1

Or if in the executer you want to store the block for later use:

def executer(&block)
@save_for_later =3D block
end

#later...
@save_for_later.call(1) #or @save_for_later[1]

Hope this helps,

Jesus.
 
J

Jeff Peng

Jesús Gabriel y Galán:
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.
 
F

Fritz Trapper

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)
 
M

Marnen Laibow-Koser

Fritz said:
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,
 
J

Jesús Gabriel y Galán

Jes=FAs Gabriel y Gal=E1n:
for &func, what's the "&" before "func" here?

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 =03:0

Jesus.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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)
def executer(obj, method)
obj.send method , 1
end

class X
def test(x)
p x
end
end

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

Albert Schlef

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top