Passing two functions as parameters

F

Frank Meyer

Is it possible to pass a function as a parameter?

I want to do something like this:

def my_fnc1
end

def my_fnc2
end



def call_fncs( function1, function2 )

#call function1
#call function2
end


#call call_fncs( my_fnc1, my_fnc2 )




Turing.
 
P

Phrogz

Is it possible to pass a function as a parameter?

irb(main):001:0> def hi; puts "hello"; end
=> nil
irb(main):002:0> def earth; puts "world"; end
=> nil
irb(main):003:0> def do2( f1, f2 )
irb(main):004:1> f1.call
irb(main):005:1> f2.call
irb(main):006:1> end
=> nil
irb(main):007:0> do2( method:)hi), method:)earth) )
hello
world
=> nil

See also lambda and Proc.new for defining anonymous functions on the
fly.
 
F

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.





Turing
 
F

Frank Meyer

Gavin said:
irb(main):001:0> def hi; puts "hello"; end
=> nil
irb(main):002:0> def earth; puts "world"; end
=> nil
irb(main):003:0> def do2( f1, f2 )
irb(main):004:1> f1.call
irb(main):005:1> f2.call
irb(main):006:1> end
=> nil
irb(main):007:0> do2( method:)hi), method:)earth) )
hello
world
=> nil

See also lambda and Proc.new for defining anonymous functions on the
fly.


Thank you that's exactly what I need.


Turing.
 
P

Peña, Botp

W21haWx0bzpsaXN0LWJvdW5jZUBleGFtcGxlLmNvbV0gT24gQmVoYWxmIE9mIEZyYW5rIE1leWVy
DQojIFN1YmplY3Q6IFJlOiBQYXNzaW5nIHR3byBmdW5jdGlvbnMgYXMgcGFyYW1ldGVycw0KIyAN
CiMgSSBrbm93IG9ubHkgb25lIHdheToNCiMgDQojIGNhbGxfZm5jcyggUHJvYy5uZXcgeyBteV9m
bmMxIH0sIFByb2MubmV3IHsgbXlfZm5jMiB9ICkNCg0KaSBsaWtlIHJ1Ynkgc2luY2UgaXQgdHJl
YXRzIHZhcnMgYW5kIG1ldGhvZHMgYWxpa2UuDQoNCnRoaXMgaXMganVzdCBhIHNpbXBsZSBleGFt
cGxlLA0KDQppcmIobWFpbik6MDAxOjA+IGRlZiB4DQppcmIobWFpbik6MDAyOjE+ICAgcHV0cyAi
eCINCmlyYihtYWluKTowMDM6MT4gZW5kDQo9PiBuaWwNCmlyYihtYWluKTowMDQ6MD4gZGVmIHkN
CmlyYihtYWluKTowMDU6MT4gICBwdXRzICJ5Ig0KaXJiKG1haW4pOjAwNjoxPiBlbmQNCj0+IG5p
bA0KaXJiKG1haW4pOjAwNzowPiBkZWYgdGVzdCh4LHkpDQppcmIobWFpbik6MDA4OjE+ICAgeA0K
aXJiKG1haW4pOjAwOToxPiAgIHkNCmlyYihtYWluKTowMTA6MT4gZW5kDQo9PiBuaWwNCmlyYiht
YWluKTowMTE6MD4gdGVzdCB4LHkNCngNCnkNCj0+IG5pbA0KaXJiKG1haW4pOjAxMjowPiBkZWYg
dGVzdDIoYT14LGI9eSxjPXgpDQppcmIobWFpbik6MDEzOjE+ICAgYQ0KaXJiKG1haW4pOjAxNDox
PiAgIGINCmlyYihtYWluKTowMTU6MT4gICBjDQppcmIobWFpbik6MDE2OjE+IGVuZA0KPT4gbmls
DQppcmIobWFpbik6MDE3OjA+IHRlc3QyDQp4DQp5DQp4DQo9PiBuaWwNCmlyYihtYWluKTowMTg6
MD4NCg0Ka2luZCByZWdhcmRzIC1ib3RwDQo=
 
G

Giles Bowkett

Is it possible to pass a function as a parameter?

You can pass a block:

kall_funk(&funk)

If you want to do the functional programming thing of feeding a
function to a function to a function, it's doable, but I don't recall
how off the top of my head. There's a great set of chapters on this in
a book called "Ruby By Example," though. "Ruby By Example" is a very
very mis-titled book. It should really be called "An Introduction To
Ruby Which Emphasizes Functional Programming" but that title would
have been a bit unwieldy.

Anyway, I know what you want to do is essentially possible, although
it sometimes looks a bit odd.
 
P

Phrogz

[mailto:[email protected]] On Behalf Of Frank Meyer
i like ruby since it treats vars and methods alike.

this is just a simple example,

irb(main):001:0> def x
irb(main):002:1> puts "x"
irb(main):003:1> end
=> nil
irb(main):004:0> def y
irb(main):005:1> puts "y"
irb(main):006:1> end
=> nil
irb(main):012:0> def test2(a=x,b=y,c=x)
irb(main):013:1> a
irb(main):014:1> b
irb(main):015:1> c
irb(main):016:1> end
=> nil
irb(main):017:0> test2
x
y
x
=> nil

You may not realize it, but what you just wrote is the same as:
def test2( a=x, b=y, c=x )
nil
nil
nil
end

The methods are being invoked as the method parameters are being set
up, and three nil values (the return value of those methods, which is
the return value of their 'puts' calls) are being assigned to your
variable. I only point this out since the code sort of implies that
the parameter assignments are treating methods as first-class
functions, and you are invoking them inside the method.
 
P

Peña, Botp

From: david karapetyan [mailto:[email protected]]=20
# I don't think what you are doing is valid code. Ruby is call=20
# by value so the arguments to a method
# get evaluated before anything happens. If your x method takes=20
# an argument then your code won't work.

arggh, what was i thinking :( i need a ptr to the fxn in ruby.=20
you are right, of course.
kind regards -botp
 
F

Frank Meyer

Thanks for all your work, but unfortunately when I created this topic I
didn't realize that I cannot use blocks, because I'm passing a Hash to
this constructor which consists of a symbol/string/whatever which maps
to an array of two functions.

So a call would look like this:

r = MyClass.new(
:symbol1 => [ method( :foo ), method( :bar ) ],
:symbol2 => [ method( :foo2 ), method( :bar2 ] ) #and so on


These functions are used to convert from a given object (can be anything
it's up to the user) to a known object type and back to a user object.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top