0

Here is my Ruby code. When I execute it, it asks the question, then when I put no or yes, nothing happens. It just ends.

puts "Do you like cats (Yes or No)?"
ans = gets.chomp

def answer(ans)
  if ans == "Yes" || ans == "yes"
    returns "Ken does too"
  elsif ans == "No" || ans == "no"
    returns "Dogs are better"
  else 
    returns "It's hard to decide"
  end
end

What am I doing wrong?

3
  • I don't see how this question is off topic or unclear. The poster just needs some help getting started with Ruby. We need to ensure the poster gets good information on how to get started instead of discouragment! Commented Oct 12, 2015 at 21:27
  • 1
    I agrée and that's why I +1 his question. Keep on learning, it will click. Commented Oct 12, 2015 at 21:53
  • Voting to close since the topic is basic knowledge covered by any tutorial. Commented Oct 13, 2015 at 2:36

4 Answers 4

3

A few corrections:

  1. You are not calling the method answer at all.
  2. Make sure you define the method before calling it.
  3. The keyword returns is undefined, it's supposed to be return.
  4. Make sure to call the method you intend to use.

Try as follows and follow a good Ruby book. Here might be a good start: https://github.com/vhf/free-programming-books/blob/master/free-programming-books.md

def answer(ans)
  if ans == "Yes" || ans == "yes"
    return "Ken does too"
  elsif ans == "No" || ans == "no"
    return "Dogs are better"
  else 
    return "It's hard to decide"
  end
end

puts "Do you like cats (Yes or No)?"
ans = gets.chomp

puts answer(ans)
Sign up to request clarification or add additional context in comments.

2 Comments

@user2066488, very nice. Enjoy Ruby.
Nicely broken down @vee
1

You did not call your answer() method anywhere. Also, returns is incorrect (return is). In Ruby, we try to avoid explicit return. You want to print the string output using puts:

 puts "Do you like cats (Yes or No)?"
    ans = gets.chomp

    def answer(ans)
      if ans == "Yes" || ans == "yes"
          puts"Ken does too"
      elsif ans == "No" || ans == "no"
          puts"Dogs are better"
      else
          puts"It's hard to decide"
      end
    end

    answer(ans)

3 Comments

I would recommend actually using print instead of puts inside of your control statement as puts creates an extra line with the output.
Right that's also a solution. What about just putting the string literally without any puts or print
That is a solution as well.
0

Using print in your control statement will avoid adding an extra line to your output.

Difference Between print and puts in ruby

def answer(ans) if ans == "Yes" || ans == "yes" print "Ken does too" elsif ans == "No" || ans == "no" print "Dogs are better" else print "It's hard to decide" end end

puts "Do you like cats (Yes or No)?" ans = gets.chomp

puts answer(ans)

Comments

-1

Use return instead of returns, or better the "ruby-way" would be this:

puts "Do you like cats (Yes or No)?"
ans = gets.chomp

def answer(ans)
  if ans == "Yes" || ans == "yes"
    "Ken does too"
  elsif ans == "No" || ans == "no"
    "Dogs are better"
  else 
    "It's hard to decide"
  end
end

puts answer(ans)

3 Comments

That will not print any string to the console since you are not calling the method answer(ans)
Based on his code, check last line of his code: puts answer(ans) / edit, oh lol sorry, it is in code from answer :/
No that's not correct, your code will not output the answer because you are not calling the method anywhere. Take a look at @vee's answer or mine...That'll help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.