[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Passing two functions as parameters

Lolz Llolz

8/16/2007 6:40:00 PM

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.
--
Posted via http://www.ruby-....

8 Answers

Phrogz

8/16/2007 6:45:00 PM

0

On Aug 16, 12:40 pm, Frank Meyer <lolz.ll...@gmail.com> wrote:
> 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.

Lolz Llolz

8/16/2007 6:47:00 PM

0

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
--
Posted via http://www.ruby-....

Lolz Llolz

8/16/2007 7:02:00 PM

0

Gavin Kistner wrote:
> On Aug 16, 12:40 pm, Frank Meyer <lolz.ll...@gmail.com> wrote:
>> 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.


Thank you that's exactly what I need.


Turing.

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

Peña, Botp

8/17/2007 2:00:00 AM

0

[mailto:list-bounce@example.com] On Behalf Of Frank Meyer
# Subject: Re: Passing two functions as parameters
#
# I know only one way:
#
# call_fncs( Proc.new { my_fnc1 }, Proc.new { my_fnc2 } )

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):007:0> def test(x,y)
irb(main):008:1> x
irb(main):009:1> y
irb(main):010:1> end
=> nil
irb(main):011:0> test x,y
x
y
=> 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
irb(main):018:0>

kind regards -botp

Giles Bowkett

8/17/2007 4:29:00 AM

0

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

--
Giles Bowkett

Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...

Phrogz

8/17/2007 4:44:00 AM

0

On Aug 16, 8:00 pm, Pe?a, Botp <b...@delmonte-phil.com> wrote:
> [mailto:list-bou...@example.com] 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.

Peña, Botp

8/17/2007 4:55:00 AM

0

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

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

Lolz Llolz

8/17/2007 2:32:00 PM

0

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.
--
Posted via http://www.ruby-....