# Stack level too deep (SystemStackError)

**URL:** <https://www.ruby-forum.com/t/stack-level-too-deep-systemstackerror/94832>\
**Category:** Ruby\
**Created:** [May 16, 2006, 2:16am UTC](https://www.ruby-forum.com/t/stack-level-too-deep-systemstackerror/94832 "2006-05-16T02:16:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 16, 2006, 2:16am UTC](https://www.ruby-forum.com/t/stack-level-too-deep-systemstackerror/94832/1 "2006-05-16T02:16:38Z")

</div>

When writing a code to find the factor of a given number, i ran into the  
following error message:

“factor.rb:48: parse error, unexpected kEND, expecting $”

I was hoping someone could shed light on the problem. Heres the code:

def isprime testNum  
testWith = 2  
run = 1  
while testNum \> testWith and run == 1  
prime = testNum % testWith  
if prime != 0  
testWith = testWith + 1  
else  
run = 2  
end  
end

```
if prime != 0
	return true
else
	return false
end

```

end

def factor toFactor  
factored = false  
numbers = Array.new  
numbers.push toFactor  
testWith = 2  
while factored == false and numbers.last \> testWith  
test = numbers.last  
isFactor = numbers.last%testWith  
if isFactor == 0  
numbers.pop  
numbers.push(testWith)  
numbers.push(test/testWith)  
factored = true  
else  
testWith = testWith + 1  
end  
end

```
eval = numbers.last
	if isprime(eval) == false
		numbers.pop
		add = factor(eval)
		numbers.push(add)
	end
end

numbers

```

end

f = factor(100)

puts f.join(’ x ')

* * *

Note: subsituting " eval = numbers.last" for "numbers.each do |eval|  
"avoids the error.

Please Help!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 16, 2006, 2:23am UTC](https://www.ruby-forum.com/t/stack-level-too-deep-systemstackerror/94832/2 "2006-05-16T02:23:26Z")

</div>

john s wrote:

> ```
> run = 1
> return true
> 
> ```
> 
> testWith = 2  
> end  
> numbers
> 
> Please Help!

I don’t think Array#last takes a block.

-Justin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 16, 2006, 8:58am UTC](https://www.ruby-forum.com/t/stack-level-too-deep-systemstackerror/94832/3 "2006-05-16T08:58:33Z")

</div>

> ```
> eval = numbers.last
> if isprime(eval) == false #
> numbers.pop #
> add = factor(eval) #unindent this bit
> numbers.push(add) #
> end #
> end # get rid of this.
> 
> ```

then it works.  
-tim

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 16, 2006, 9:47am UTC](https://www.ruby-forum.com/t/stack-level-too-deep-systemstackerror/94832/4 "2006-05-16T09:47:13Z")

</div>

The last bit could be changed to:  
print f.map{|x|x.is\_a?(Array) ? “(#{x.join(” x “)})” : x}.join(" x ")

To add the correct brackets.

J`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)
