7stud -- wrote:

> Terry Michaels wrote in post #993440:
> > I did some googling to find out if Ruby supports tail call optimization,
> > as I wanted to write tail-recursive functions. All posts I found
> > indicated that 1.9 supports this, but it is not enabled by default.
> > However, all those posts were about two years old, so I'm wondering what
> > is the current situation: do I still need to upgrade to 1.9 to get that
> > capability? Or is there something in 1.8 I can enable?
> 
> Couldn't you test that by creating two recursive functions: one with a 
> tail call and one without, and then benchmarking them?

def fib n, a = 0, b = 1
  if n == 0
    b
  else
    a, b = b, a + b
    fib n - 1, a, b
  end
end

1.upto(9){|n|
  p [n, fib(n)]
}

p fib( 50_000 )

  ==>

[1, 1]
[2, 2]
[3, 3]
[4, 5]
[5, 8]
[6, 13]
[7, 21]
[8, 34]
[9, 55]
try4.rb:23:in `fib': stack level too deep (SystemStackError)
        from try4.rb:23:in `fib'
        from try4.rb:31


irb(main):004:0> VERSION
=> "1.8.7"