What am I doing wrong in this Ruby code? - ruby

def pregunta
reply = gets.chomp.downcase
if reply == 'si'
puts 'Correcto'
end
if reply == 'no'
puts 'Incorrecto'
end
end
pregunta 'alfkjdasñlfj?'
After I ask a question ("pregunta" is question in english), the program should tell me if I'm right ("correcto") or wrong ("incorrecto") when I enter an answer ("si" or "no"). Instead of getting an answer, I get an "ArgumentError". What am I doing wrong?
I'm using Ruby 1.9.3-p194

pregunta doesn't take a parameter, but you pass one anyways. I might be wrong, but wasn't what you were trying to do something like this:
def pregunta(preg)
puts preg
reply = gets.chomp.downcase
if reply == 'si'
puts 'Correcto'
end
if reply == 'no'
puts 'Incorrecto'
end
end

You're not passing in a value to the function call, you're reading it from the terminal. Call the function and then enter in your string.
pregunta
si
Correcto
=> nil

You don't have any parameters in your method declaration.
You'll need something like this:
def pregunta(foo)
if foo == 'bar'
true
end
end

You are calling pregunta 'alfkjdasñlfj?' but that method has no arguments.
def pregunta(answer)
reply = answer.chomp.downcase
if reply == 'si'
puts 'Correcto'
end
if reply == 'no'
puts 'Incorrecto'
end
end
pregunta gets
maybe ?

Related

Ruby undefined variable

I have a code below:
secret_number = 8
user_input = ""
def number_guesser(user_input)
while user_input != secret_number
puts "Guess a number between 1 and 10:"
user_input = gets.chomp
if user_input != secret_number
puts "Wrong! Try again."
else
puts "You guessed correctly!"
end
end
end
number_guesser(user_input)
when I tried to run the above program it showed as below:
****undefined local variable or method secret_number' for main:Object
(repl):211:innumber_guesser'
(repl):221:in `'****
Any ideas?
You can't use a local variable like that inside another scope such as a method, it's two different contexts. Instead you need to pass that in if you want to use it.
It's a simple change:
def number_guesser(user_input, secret_number)
# ...
end
Then just feed that argument in.
You'll note that user_input isn't really necessary as a parameter, you can always initialize and use that locally, so it's actually pointless as an argument.
The pattern to use in that case:
loop do
input = gets.chomp
# Prompting...
break if input == secret_number
# Guessed wrong...
end

How can I make script work in Ruby?

I am new to Ruby.
I need to make this script work:
puts "Do you like cats?"
ask = gets
def ask(n)
if ask == yes
return "I do too"
end
if ask == no
return "Dogs are better"
end
end
puts "#{ask(n)}"
Error message is :
pracif.rb:15:in <main>': undefined local variable or methodn' for
main: Object (NameError)
Here's a script that would work for you :
puts "Do you like cats?"
answer = gets
def ask(n)
if n == 'yes'
return "I do too"
end
if n == 'no'
return "Dogs are better"
end
end
puts ask(answer.downcase.chomp)
Explaination
As the error said you were trying to pass in a variable n which was not defined
Secondly you have a method name ask same as variable name. I've renamed the variable to answer instead
Thirdly, enclose yes and no in quotes
And finally, since you are using gets a \n gets appended like yes\n so none of your conditions would match. So i've used chomp to remove \n. And also used downcase to make input case insensitive.
EDIT
As mentioned by #Jordan in the comments, there is no reason to use string interpolation for the puts statement. So it's enough to call the method directly.
There are a bunch of issues with your code. Try something more like:
def reply(response)
return 'I do too' if response == 'yes'
return 'Dogs are better' if response == 'no'
'Invalid response!'
end
puts 'Do you like cats?'
response = gets().chomp()
puts reply(response)
Pay attention to the variable names. If you keep them descriptive, it is easier to spot mistakes.
Your script has no n local variable defined that you are passing to your ask(n) method at the end.
Rename your ask variable that your script gets from user to answer for example and pass it to your ask method at the end like so:
Updated code to fix other problem I did not see in the first run.
puts "Do you like cats?"
answer = gets.chomp
def ask(n)
(n == 'yes') ? "I do too" : "Dogs are better"
end
puts "#{ask(answer)}"

Basic Ruby Programming

I have the following ruby code, from a learning to program book. I understand it ,however it asks me to try and remove the variable good_answer and answer. It says I will have to use return to exit the loop. I am not sure where to start. Any clues would be great, I just want to figure it out.
def ask question
good_answer = false
while (not good_answer)
puts question
reply = gets.chomp.downcase
if (reply == 'yes' or reply == 'no')
good_answer = true
if reply == 'yes'
answer = true
else
answer = false
end
else
puts 'Please answer "yes" or "no".'
end
end #while ends here
answer #This is what we return (true of false)
end
puts
ask('Do you like eating Cheese?')
ask('Are you crazy?')
rain = ask 'Do you like rain?'
puts rain
I would just do what it says - put return in where answer is being used:
def ask question
while (true)
puts question
reply = gets.chomp.downcase
if (reply == 'yes' or reply == 'no')
if reply == 'yes'
return true
else
return false
end
else
puts 'Please answer "yes" or "no".'
end
end #while ends here
end

ruby program basic design explanation

I want to rewrite the following code with the code below, but I am stuck.
def ask question
good_answer = false
while (not good_answer)
puts question
reply = gets.chomp.downcase
if (reply == 'yes' or reply =='no')
good_answer = true
if reply == 'yes'
answer = true
else
answer = false
end
else
puts 'Please answer "yes" or "no"'
end
end
answer
end
Replacement code:
def ask question
puts question
reply = gets.chomp
if (reply == 'yes' or reply == 'no')
puts reply.capitalize
else
puts 'Please enter "yes" or "no"'
#jump the code to like 2 ( but how?)- use while reply != empty & comment the below lines
puts question
reply = gets.chomp
end
end
I want to jump to the main part of program is there any goto, jump or can I call method inside that method?
I want to jump to the main part of program is there any goto, jump or can I call method inside that method?
Yes, it is called a loop, i.e., what you are using in your original code. Why in the world would you want to replace a loop with a goto? Makes no sense.
It can however be simplified. I don't like the checking against 'yes' or 'no', but I also don't have time to restructure your program.
def ask question
while true
puts(question)
reply = gets.chomp.downcase
if reply == 'yes' || reply == 'no'
return reply == 'yes'
else
puts('Please answer "yes" or "no"')
end
end
end
Even if there was a goto statement you shouldn't be using it. Not only is it bad form, but it causes all kinds of headaches for maintainers since your program ends up being hard to follow.
A better approach is to define proper structures for your questions and valid answers, then iterate over those simply, collecting the results into a structure you can use later:
# Auto-flush output buffer
STDOUT.sync = true
questions = [
[ 'Is this a good question?', 'yes', 'no' ],
[ 'Is the sky blue?', 'yes', 'no' ],
[ 'Do hamsters fly?', 'no', 'yes' ]
]
answers_given = [ ]
questions.each do |question, *answers|
print question + ' '
while (true)
answer = gets
answer.chomp!
if (answers.include?(answer))
puts "Thanks!"
answers_given << (answer == answers.first)
break
end
puts "You must answer one of #{answers.join(', ')}!"
print question + ' '
end
end
questions.each_with_index do |(question, *answers), i|
puts "#{question} #{answers_given[i]}"
end
You can try something liek this:
def ask_question
puts('Please answer "yes" or "no"') until (reply = gets.chomp.downcase) =~ /^(yes|no)$/
return reply == 'yes'
end
def ask question
puts question
reply = gets.chomp.downcase
if (reply == 'yes' or reply == 'no')
puts reply.capitalize
else
puts 'Please enter "yes" or "no"'
ask question # this does the looping of loop
end
end
Thanks, and sorry I didn't copy it well from my clipboard last time.

Ruby Loops with Grandma

Okay, I'm trying to write a ruby simulation of my grandmother. I can't quite get the loop to work the way I'd like though. I want granny to respond with
"OH, THAT REMINDS ME OF BACK IN (random year) ..."
when you answer her in all caps but I also want her to respond with
"WHAT'D YOU SAY????"
when you don't use all caps. I can get each one to work separately but I can't seem to make a continuous loop of granny with her crazy responses. Here's the code:
puts 'HELLO SONNY! WHAT\'S NEW IN THE WHO\'S IT WHAT\'S IT?'
response = gets.chomp
while response == response.upcase
puts 'OH, THAT REMINDS ME OF BACK IN ' + (rand(50) + 1905).to_s + '...'
response = gets.chomp
end
while response != response.upcase
puts 'WHAT\'D YOU SAY????'
response = gets.chomp
end
Any ideas?
The issue is that once you exit the first while loop, you're never returning back to it. Try something like this:
while true
response = gets.strip
if response == response.upcase
puts msg1
else
puts msg2
end
end
That'll run forever, until you decide to kill virtual-granny with Ctrl-C.
This program works, though Im a noob so it may not be the best way. Also my math is more creative then practical, the other guys is way better. :)
puts 'Talk to your grandma!'
while true
say = gets.chomp
if say == say.downcase
puts 'WHAT DID YOU SAY? SPEAK UP!'
else say == say.upcase
puts "NO HONEY, NOT SINCE 19" + (rand(90) + 10).to_s
end
break if say == 'bye'.upcase
end

Resources