Passing two functions as parameters

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

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

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.

···

On Aug 16, 12:40 pm, Frank Meyer <lolz.ll...@gmail.com> wrote:

Is it possible to pass a function as a parameter?

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.blogspot.com
Portfolio: http://www.gilesgoatboy.org

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

[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

On Behalf Of 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.

ok, let me take a second shot (and my apology to my previous post).

i find when doing thing like this in ruby, you should think about blocks, block, it's blocks everywhere. blocks allows one to think (in a cleaner) way between being oo-oriented or fxn-oriented.

when in ruby, i think pass object, when in javascript, i think pass fxns (yes, yes, i drunk too much javascript last month; hey is passing fxns in javascipt really addictive? :slight_smile:

anyway, this is another simple example. hopefully, this time i am correct :slight_smile:

irb(main):001:0> def x
irb(main):002:1> puts "x"
irb(main):003:1> "x"
irb(main):004:1> end
=> nil
irb(main):005:0> def y
irb(main):006:1> puts "y"
irb(main):007:1> "y"
irb(main):008:1> end
=> nil
irb(main):009:0>
irb(main):010:0* def test(p1=lambda{x},p2=lambda{y},p3=p1)
irb(main):011:1> p1.call + p2.call + p3.call
irb(main):012:1> end
=> nil
irb(main):013:0> test
x
y
x
=> "xyx"

hmmm, no surprises there. but you're right, it isn't so clean...

so let's introduce blocks,...

irb(main):014:0> def test2
irb(main):015:1> yield
irb(main):016:1> end
=> nil
irb(main):018:0> test2 {x}
x
=> "x"
irb(main):019:0> test2 {y}
y
=> "y"

hmmm, not bad. I didn't declare any lambda or proc in there.. very clean.. it's as if yield is doing something additional job for me (and i like it) ..

now, let's see if we can so some ala fxnal styles using blocks,...

irb(main):035:0* def test3 p1=:x
irb(main):036:1> if p1 == :x
irb(main):037:2> test2 {x}
irb(main):038:2> else
irb(main):039:2* test2 {y}
irb(main):040:2> end
irb(main):041:1> end
=> nil
irb(main):042:0> test3
x
=> "x"
irb(main):043:0> test3 :y
y
=> "y"

hmmm, not bad...
but can we skip the test2 call so we can be "more" fxnal, so to speak ?
... ok

irb(main):044:0> def test4 p1=:x
irb(main):045:1> if p1 == :x
irb(main):046:2> yield lambda{x}
irb(main):047:2> else
irb(main):048:2* yield lambda{y}
irb(main):049:2> end
irb(main):050:1> end
=> nil
irb(main):051:0> test4 {|a| a.call}
x
=> "x"
irb(main):052:0> test4(:y) {|a| a.call}
y
=> "y"
irb(main):053:0>

Is that ok?
kind regards -botp

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.

···

On 8/16/07, Peña, Botp <botp@delmonte-phil.com> wrote:

[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

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.

···

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

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-forum.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 :frowning: i need a ptr to the fxn in ruby.
you are right, of course.
kind regards -botp

···

From: david karapetyan [mailto:dkarapetyan@gmail.com]