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.
D10828d989e994a99c63f1b0d88c2a7e?d=identicon&s=25 john s (Guest)
on 2006-05-16 02: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!
F3b7109c91841c7106784d229418f5dd?d=identicon&s=25 Justin Collins (justincollins)
on 2006-05-16 02: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
62002cee15efcf4628cd7efc19425a07?d=identicon&s=25 Tim Becker (Guest)
on 2006-05-16 08: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
5d15f83f0e681b138d95f1ec430868fa?d=identicon&s=25 Joey (Guest)
on 2006-05-16 09: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.