Hi,
If I’ve written a class method that calls itself recursively is there a
problem or any difference between doing something like this:
class << self
def mymeth( n )
blah blah…
mymeth( n + 1)
end
end
as opposed to this?
class << self
def mymeth( n )
blah blah…
Classname.mymeth( n + 1 )
end
end
I wrote something a few minutes ago in the first way, and it works
(those virgin sacrifices must be working their magic) but wondered if
that’s A Really Bad Thing To Do, or if it’s more like wearing socks with
sandals on a warm day?
Regards,
Iain
On Wed, Oct 13, 2010 at 1:24 PM, Iain B. [email protected]
wrote:
end
end
I wrote something a few minutes ago in the first way, and it works (those virgin
sacrifices must be working their magic) but wondered if that’s A Really Bad Thing
To Do, or if it’s more like wearing socks with sandals on a warm day?
Even simpler
irb(main):006:0> class X
irb(main):007:1> def self.r(n) p n; r(n-1) if n > 0 end
irb(main):008:1> end
=> nil
irb(main):009:0> X.r 5
5
4
3
2
1
0
=> nil
You do not need “Classname.” because “self” is the class instance.
Actually placing the class name could do more harm than good (think of
“private” and renaming the class).
Kind regards
robert
On 13 Oct 2010, at 12:43, Robert K. wrote:
You do not need “Classname.” because “self” is the class instance.
Actually placing the class name could do more harm than good (think of
“private” and renaming the class).
Kind regards
robert
Ok, thanks very much. Nice to know my natural way was the best way ![:slight_smile: :slight_smile:](/images/emoji/apple/slight_smile.png?v=6)
Regards,
Iain