define a function inside a method

O

Olivier Renaud

Hi,

I wrote a method which uses recursion, internally. As a test, I tried to
define the recursive function inside the method that is called :

class Ga
def bu
def zo(n)
# recursively call zo
end
10.times {|n| zo(n)}
end

At my surprise, this code ran well ! But I wonder if this is a good idea...
Should I be aware of possible problems, or limitations, coming from this
kind of construction ? I can't figure out where the zo function
actually "lives".

Thanks.
 
R

Robert Klemme

Hi,

I wrote a method which uses recursion, internally. As a test, I tried to
define the recursive function inside the method that is called :

class Ga
def bu
def zo(n)
# recursively call zo
end
10.times {|n| zo(n)}
end

At my surprise, this code ran well ! But I wonder if this is a good idea...
Should I be aware of possible problems, or limitations, coming from this
kind of construction ? I can't figure out where the zo function
actually "lives".

The issue here is (apart from the missing "end" in your piece above)
that zo will be defined every time you execute bu - that's probably not
something you want as it is inefficient and you do not actually change
zu's definition. Also, zo becomes a normal method - there is no such
thing as a nested method in Ruby:

irb(main):001:0> class Ga
irb(main):002:1> def bu
irb(main):003:2> def zo(n)
irb(main):004:3> # recursively call zo
irb(main):005:3* end
irb(main):006:2> 10.times {|n| zo(n)}
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> Ga.new.zo 1
NoMethodError: undefined method `zo' for #<Ga:0x39ba98>
from (irb):9
from :0
irb(main):010:0> Ga.new.bu
=> 10
irb(main):011:0> Ga.new.zo 1
=> nil

Kind regards

robert
 
O

Olivier Renaud

Le samedi 03 mars 2007 16:10, Robert Klemme a =E9crit=A0:
The issue here is (apart from the missing "end" in your piece above)
that zo will be defined every time you execute bu - that's probably not
something you want as it is inefficient and you do not actually change
zu's definition. Also, zo becomes a normal method - there is no such
thing as a nested method in Ruby:

irb(main):001:0> class Ga
irb(main):002:1> def bu
irb(main):003:2> def zo(n)
irb(main):004:3> # recursively call zo
irb(main):005:3* end
irb(main):006:2> 10.times {|n| zo(n)}
irb(main):007:2> end
irb(main):008:1> end
=3D> nil
irb(main):009:0> Ga.new.zo 1
NoMethodError: undefined method `zo' for #<Ga:0x39ba98>
from (irb):9
from :0
irb(main):010:0> Ga.new.bu
=3D> 10
irb(main):011:0> Ga.new.zo 1
=3D> nil

Kind regards

robert

Thanks, this is perfectly clear !
=46inally, I rewrote this method to avoid the recursion, since it was a tai=
l=20
recursion.

=2D-=20
Olivier Renaud
 
P

Phrogz

I wrote a method which uses recursion, internally. As a test, I tried to
define the recursive function inside the method that is called :

Time to investigate:

class Foo
def bar
p jim rescue p "no jim yet"
def jim; "jim"; end
jim
end
end

f1 = Foo.new

p f1.jim rescue p "no jim yet"
#=> "no jim yet"

p f1.bar
#=> "no jim yet"
#=> "jim"

p f1.jim
#=> "jim"

f2 = Foo.new
p f2.jim
#=> "jim"

p f1.method( :jim )
#=> #<Method: Foo#jim>


What I deduce from this is that running Foo#bar causes a new instance
method "jim" to be defined for the Foo class. The only thing this gets
you is delayed method realization, once only, per class. It's not a
private function for the use by that method only.
 
P

Pit Capitain

Olivier said:
I wrote a method which uses recursion, internally. As a test, I tried to
define the recursive function inside the method that is called :

class Ga
def bu
def zo(n)
# recursively call zo
end
10.times {|n| zo(n)}
end

In addition to the other answers, if you really want method-internal
methods, you can use lambdas:

class Ga
def bu
zo = lambda {|n|
# recursively call zo
}
10.times {|n| zo.call(n)}
end
end

Regards,
Pit
 

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,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top