On Wed, Oct 13, 2010 at 1:24 PM, Iain Barnett <iainspeed / gmail.com> wrote:
> Hi,
>
> If I've written a class method that calls itself recursively is there a p=
roblem or any difference between doing something like this:
>
> class << self
> =A0def mymeth( n )
> =A0 =A0blah blah...
>
> =A0 =A0mymeth( n + 1)
> =A0end
> end
>
>
> as opposed to this?
>
> class << self
> =A0def mymeth( n )
> =A0 =A0blah blah...
>
> =A0 =A0Classname.mymeth( n + 1 )
> =A0end
> 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 Re=
ally Bad Thing To Do=99, 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
=3D> nil
irb(main):009:0> X.r 5
5
4
3
2
1
0
=3D> 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/