[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to pass a function as argument in ruby?

the_crazy88

2/12/2006 9:21:00 PM

I've searched a bit about the subject, but I don't understand it. All I
want is to simply pass a function of one instance to another. In Python
this works, but in ruby not.

Python code would be:
def x(a,b):
return a+b

def y(a):
return a(3,4)

y(x)
# Result is 7
But the equivalent ruby code doesn't work:

def a(x,y)
return x+y
end

def x(b)
return b(3,4)
end

x(a)
#Gives the following error in irb:

NoMethodError: undefined method `b' for main:Object
from (irb):40:in `x'
from (irb):42
from :0

8 Answers

dblack

2/12/2006 9:36:00 PM

0

James Gray

2/12/2006 9:38:00 PM

0

On Feb 12, 2006, at 3:23 PM, the_crazy88 wrote:

> But the equivalent ruby code doesn't work:
>
> def a(x,y)
> return x+y
> end
>
> def x(b)
> return b(3,4)
> end
>
> x(a)
> #Gives the following error in irb:

You can ask for a method object and call it when needed:

def a(x,y)
return x+y
end

def x(b)
return b.call(3,4)
end

p x(method(:a))

Hope that helps.

James Edward Gray II


Simon Kröger

2/12/2006 9:42:00 PM

0

the_crazy88 wrote:
> I've searched a bit about the subject, but I don't understand it. All I
> want is to simply pass a function of one instance to another. In Python
> this works, but in ruby not.

def a(x,y)
return x+y
end

def x(b)
return b.call(3,4)
end

puts(x(method(:a)))

# or the ruby way:

def x
yield 3, 4
end

puts(x{|a, b| a + b})

cheers

Simon


Tim Hunter

2/12/2006 9:46:00 PM

0

the_crazy88 wrote:
> I've searched a bit about the subject, but I don't understand it. All I
> want is to simply pass a function of one instance to another. In Python
> this works, but in ruby not.
>
> Python code would be:
> def x(a,b):
> return a+b
>
> def y(a):
> return a(3,4)
>
> y(x)
> # Result is 7
> But the equivalent ruby code doesn't work:
>
> def a(x,y)
> return x+y
> end
>
> def x(b)
> return b(3,4)
> end
>
> x(a)
> #Gives the following error in irb:
>
> NoMethodError: undefined method `b' for main:Object
> from (irb):40:in `x'
> from (irb):42
> from :0
>

In Ruby it's typical to encapsulate bits of code in Proc objects and
pass these objects around:

p = Proc.new {|x,y} x+y}

p.call(2,3) -> 5

If you're really wanting to pass methods around from one instance to
another, check out the doc for the UnboundMethod class with ri.

David Vallner

2/12/2006 9:53:00 PM

0

Dna Nedela 12 Február 2006 22:48 Timothy Hunter napísal:
> the_crazy88 wrote:
>
> In Ruby it's typical to encapsulate bits of code in Proc objects and
> pass these objects around:
>
> p = Proc.new {|x,y} x+y}
>
> p.call(2,3) -> 5
>
> If you're really wanting to pass methods around from one instance to
> another, check out the doc for the UnboundMethod class with ri.

Or use Procs and inject the instance into them as a parameter, which is a bit
more readable to me.

David Vallner


the_crazy88

2/13/2006 8:53:00 AM

0

Ok, thanks a lot everyone! Seems like a lot of usefull ideas.

the_crazy88

2/14/2006 5:59:00 AM

0

Though I tried your suggestions, something worked out much better. I
just needed to pass a reference to the calling class with the argument
self. That way I can call back functions just by executing them. For
example:

class Example
attr_reader :show_info
def initialize
@instance = self
@example2 = Example2.new(@instance)
end
def show_info(text)
puts text
end
end

class Example2
def initialize(instance)
@instance = instance
@instance.show_info("Yes, this worked out :D")
end
end

David Vallner

2/14/2006 9:04:00 PM

0

Dna Utorok 14 Február 2006 07:03 the_crazy88 napísal:
> Though I tried your suggestions, something worked out much better. I
> just needed to pass a reference to the calling class with the argument
> self. That way I can call back functions just by executing them. For
> example:
>

There you go, proper OO instead of functional for the heck of it.

> class Example
> attr_reader :show_info
The above line of code is completely unnecessary.
> (snip)


David Vallner