# Stack level too deep

**URL:** <https://www.ruby-forum.com/t/stack-level-too-deep/154703>\
**Category:** Ruby\
**Created:** [December 3, 2008, 4:40am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/154703 "2008-12-03T04:40:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Ky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@John\_Ky](https://www.ruby-forum.com/u/John_Ky)\
**Post date:** [December 3, 2008, 4:40am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/154703/1 "2008-12-03T04:40:36Z")

</div>

Hi,

Is there any way for me to increase the stack size? My stack is huge:

playback.rb:38:in `play\_back’: stack level too deep (SystemStackError)

Cheers,

-John

---

<div class="post-metadata">

**Author:** ![John\_Ky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@John\_Ky](https://www.ruby-forum.com/u/John_Ky)\
**Post date:** [December 3, 2008, 4:58am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/154703/2 "2008-12-03T04:58:08Z")

</div>

By the way, I’m sure it’s not infinite recursion. It’s just the nature  
of  
my algorithm:

```
  class PlaybackBase
     def record(&block)
        return PlaybackChain.new(self, block)
     end
  end

  class PlaybackNil < PlaybackBase
     def initialize(root)
        @root = root
     end

     def play_back(&block)
        block.call(@root)
     end
  end

  class PlaybackChain < PlaybackBase
     def initialize(tail, block)
        @tail = tail
        @block = block
     end

     def play_back(&block)
        @block.call(proc { @tail.play_back(&block) })
     end
  end

```

And it’s better that I use the system stack rather than my own because  
it  
preserves nice behaviours with blocks

$array1 = [1, 2, 3]  
$array2 = [‘a’, ‘b’, ‘c’]

$tail = PlaybackNil.new(nil)

$tail = $tail.record do |block|  
$array1.each do |value|  
$value1 = value  
block.call  
end  
end

$tail = $tail.record do |block|  
$array2.each do |value|  
$value2 = value  
block.call  
end  
end

$tail.play\_back do  
puts “#{$value1}, #{$value2}”  
end

Above prints

1, a  
2, a  
3, a  
1, b  
2, b  
3, b  
1, c  
2, c  
3, c

I build long playback chains like this to replay different combinations  
of  
things.

-John

---

<div class="post-metadata">

**Author:** ![John\_Ky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@John\_Ky](https://www.ruby-forum.com/u/John_Ky)\
**Post date:** [December 3, 2008, 5:15am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/154703/3 "2008-12-03T05:15:40Z")

</div>

Hi John,

Just a friendly suggestion, but this and your previous query about  
blocks make me wonder if you shouldn’t be using a functional language?  
Ruby has very convenient functional syntax but doesn’t (at least not  
to my knowledge) do the same sorts of optimizations that more purely  
functional languages will do.

The other option (as I’m watching PragDave’s RubyConf talk on 1.9) is  
to use Fibers. Watch the bit of his talk on Fibers at  
[http://rubyconf2008.confreaks.com/ruby-19-what-to-expect.html](http://rubyconf2008.confreaks.com/ruby-19-what-to-expect.html)  
starting about 18 min in.

Cheers,

Josh

---

<div class="post-metadata">

**Author:** ![John\_Ky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@John\_Ky](https://www.ruby-forum.com/u/John_Ky)\
**Post date:** [December 3, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/stack-level-too-deep/154703/4 "2008-12-03T18:45:20Z")

</div>

John Ky wrote:

> Hi,
> 
> Is there any way for me to increase the stack size? My stack is huge:
> 
> playback.rb:38:in `play\_back’: stack level too deep (SystemStackError)

ulimit -s? [there’s also a patch coming soon for 1.8.7 to help with  
stack size–stay tuned, I guess–discussed in  
[http://www.ruby-forum.com/topic/170608#new](http://www.ruby-forum.com/topic/170608#new)]  
Cheers!  
-=R

---

<div class="post-metadata">

**Author:** ![John\_Ky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@John\_Ky](https://www.ruby-forum.com/u/John_Ky)\
**Post date:** [December 5, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/stack-level-too-deep/154703/5 "2008-12-05T14:42:25Z")

</div>

> Sadly, it didn’t work. Instead of getting a system stack error at 3467  
> levels, I’m now getting the same at 61 levels. Do I not get a new stack  
> with a new thread?

Not with 1.8. I might query the ruby core group and see if they have  
any insight.  
-=R

---

<div class="post-metadata">

**Author:** ![John\_Ky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@John\_Ky](https://www.ruby-forum.com/u/John_Ky)\
**Post date:** [December 5, 2008, 9:18am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/154703/6 "2008-12-05T09:18:03Z")

</div>

Hi Josh,

Fibres do sound interesting.

Anyhow, I’m stuck with 1.8 at the moment. I had this idea that I could  
spawn another thread with a fresh stack and continue the excution on  
that.  
Sadly, it didn’t work. Instead of getting a system stack error at 3467  
levels, I’m now getting the same at 61 levels. Do I not get a new stack  
with a new thread?

Thanks

-John

---

<div class="post-metadata">

**Author:** ![John\_Ky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@John\_Ky](https://www.ruby-forum.com/u/John_Ky)\
**Post date:** [December 7, 2008, 4:47am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/154703/7 "2008-12-07T04:47:33Z")

</div>

Hi Roger,

It looks as if I create my threads earlier when the stack isn’t so full  
gives me threads that have more stack space. So now, I create a thread  
pool  
at the beginning of my program and use those threads rather than create  
the  
threads at the point where I know I am running out of stack space.

-John
