1
def getPrime(testNumber) temp1=testNumber -1; bIsPrime = false; while (temp1 > 1) do bIsPrime = ((testNumber % temp1) == 0) puts("Check 1 #{((testNumber % temp1) == 0)}, temp1=#{temp1} "); if ($bIsPrime) break; else temp1 = temp1-1; end end return bIsPrime end puts("Enter any number to know if it's Prime or not"); testNumber = gets() returnVal = getPrime(Integer(testNumber)); puts("Is enternered number Pime? #{return}"); 

I have just started with Ruby...So to begin with i tried to write a prime number program.

This thing is giving error "unexpected return"

Any help would be great. Thanks.

2
  • 2
    Welcome to Stack Overflow. Remember to upvote answers you find helpful. And be sure to "check" the answer that best answers your question. Commented Sep 20, 2011 at 3:35
  • you may want to look at Ruby's built in Prime library. Commented Sep 21, 2011 at 2:46

3 Answers 3

3

return is reserved. You cannot use it as a variable name, or otherwise, other than to return.

I believe you meant puts("Is entered number prime? #{!returnVal}");

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Sorry i actually meant "${!returnVal}"
2

Don't check this answer, Adam was first. But here's more info

As Adam says in his answer, the problem is that you said

puts("Is enternered number Pime? #{return}"); 

What was happening

Ruby evaluates whatever is inside the #{ foo } construct--if it is in an interpolated string, such as

puts "1 + 1 = #{1+1}" ==>> Will print 1 + 1 = 2

The resulting value is then converted (coerced) to be a string.

In your case, you told ruby to evaluate the return statement, which didn't make any sense in the context. Hence the slightly weird error message.

Comments

0

although you already have your answer I thought it would be helpful to drop this here, it follows the same idea you're using in your code:

def is_prime?(test_number) (2..test_number-1).reverse_each do |i| divisible = (test_number % i == 0) puts "Check ##{test_number-i}, divisible=#{divisible}, temp=#{i}" return false if divisible end return true end puts "Enter any number to know if it's prime or not" test_number = gets.chomp.to_i puts "Is entered number prime? #{is_prime?(test_number)}" 

2 Comments

Thanks drep :) I was looking for your answer.. But i have other doubts as well on your answer... 1. "is_prime?" does adding a ? after the function name makes any difference? 2. What is the use of ## ? Thanks a lot for your reply :)
(1) Using ? at the end doesn't have any impact in the result, you can read more on this (and bang(!) methods) here: rubylearning.com/satishtalim/writing_own_ruby_methods.html (2) the first # is just a normal string so it displays Check #1... Check #2. One last thing, although I appreciate you picking my reply, mine wasn't really an answer but rather an addition so you could see how to write your code in a more ruby-like manner, should have picked Adam's or Larry's. If you can change it do so, otherwise keep it in mind for next time and glad I could help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.