# StackError: stack level too deep

**URL:** <https://www.ruby-forum.com/t/stackerror-stack-level-too-deep/54527>\
**Category:** Ruby\
**Created:** [March 7, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/stackerror-stack-level-too-deep/54527 "2006-03-07T20:26:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 7, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/stackerror-stack-level-too-deep/54527/1 "2006-03-07T20:26:16Z")

</div>

i’m currently learning Ruby. So, while learning about code blocks and  
yields i wanted to put my freshly acquired knowledge to the test and  
(just to see if i understood correctly) write my own simple each method  
for Arrays. so i did:

class Array  
def each  
for x in self  
yield(x)  
end  
end  
end

But running it gives me SystemStackError: stack level too deep. It works  
fine when i rename it, so i guess it’s just Ruby not appreciating my  
fine work or somehow making sure i don’t introduce flagrant overwrites  
to built-in methods??? Anybody feels like enlightening me on how this  
works? Thanks.

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 7, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/stackerror-stack-level-too-deep/54527/2 "2006-03-07T20:46:30Z")

</div>

On Tuesday 07 March 2006 13:26, Sebastian F. wrote:

> end  
> end
> 
> But running it gives me SystemStackError: stack level too deep. It works  
> fine when i rename it, so i guess it’s just Ruby not appreciating my  
> fine work or somehow making sure i don’t introduce flagrant overwrites  
> to built-in methods??? Anybody feels like enlightening me on how this  
> works? Thanks.

If I’m not mistaken, the “for foo in bar” construct uses the each  
method. (It  
basically gets translated to “bar.each do |foo|”

So, your method now looks like:

def each  
self.each do |x|  
yield(x)  
end  
end

This keeps recursing until ruby kills it due to the stack being too  
deep.
