Forum: Ruby stack level too deep (SystemStackError)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
john s (Guest)
on 2006-05-16 04:16
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!
Justin C. (Guest)
on 2006-05-16 04:23
(Received via mailing list)
john s wrote:
> 		run = 1
> 		return true
> 	testWith = 2
> 		end
> 	numbers
>
> Please Help!
>
>

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

-Justin
Tim B. (Guest)
on 2006-05-16 10:58
(Received via mailing list)
>         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
Joey (Guest)
on 2006-05-16 11:47
(Received via mailing list)
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
This topic is locked and can not be replied to.