Ruby returning "goodbye" unexpectedly - ruby

So I wrote this code:
while true
text = gets
if text == "goodbye"
puts "goodbye"
break
end
end
(I wrote it with indentation)
And when I type: Hello
It returns "goodbye" and the loop keeps on going?
Could somebody explain why?
And how to fix it?
I typed "goodbye" it returns "goodbye" and keeps on going.

You likely need to use gets.chomp because it will include a \n character on the string. I recommend you use binding.pry for debugging things like this

I think you should take in count that gets catches the "\n".
So you can use:
break if text.include?('goodbye')
or
break if text.chomp.eql?('goodbye')

Related

Ruby- gets issue

I'm currently in the process of learning ruby, and I don't know if I'm doing something wrong or the compiler is, but this code:
puts "Name?"
name = gets
puts "Welcome " + name
Outputs:
#blank line waiting for input, if gotten input
Prints input, Name? And Welcome Name
I want it to do something like python's input("Name? ")
You can write your own Python equivalent input method:
def input(prompt)
print(prompt) # Output prompt
$stdout.flush # Flush stdout buffers to ensure prompt appears
gets.chomp # Get user input, remove final newline with chomp
end
Now we can try it:
name = input('What is your name? ')
puts "Welcome #{name}"
For more information on the methods used here. See these:
IO.flush
String.chomp

Why does the text printed after my variable appear on a new line?

puts "Hey what's your name?!!"
name = gets
puts "#outputNAME: #{name}"
puts "Hey #{name}! Howdy doin'!"
puts "Tell us 2 numbers that add up to your age!"
age1 = gets.to_i
age2 = gets.to_i
puts "Hey! You're #{age1 + age2} years old! Gotcha!"
puts "I'm totally new to Ruby on Rails, so thanks for running my first Ruby
Program. Please do leave an upvote and I'll appreciate that."
puts "If you think I need a downvote, please do state, in the comments
section, what I could have done to make my program better (my 1st program,
remember that #{name}). Also, check out my HTML pages, as I'm an ace at
HTML."
print "Thanks, once again #{name} and stay tuned (especially for my HTML
projects)!"
In this part, after #{name}, the sentence is continued on a new line, which I want not to happen. Is there any explanation as to what could be the error in my program, or is it an error in the language? Please help me, as I'm doing an individual project.
Thanks to whoever does find the solution to this problem.
it prints text to the new line after name, because you are accepting newline character in the following code.
name = gets
name = gets
raj
=> "raj\n"
you can remove such newline character by chomp method.
name = gets.chomp
raj
=> "raj"

How to display modified string?

I am creating a Daffy Duck speech converter (Very simple. Straight from CodeCademy) and I am having an issue with displaying the modified entry from the user.
Code:
puts "What would you like to convert to Daffy Duck language?"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
print #{user_input}
else puts "I couldn't find any 's' in your entry. Please try again."
end
It will change any 's' in your entry to a 'th', therefore, making it sound like a Daffy Duck once read aloud. When I enter it into the interpreter, it will not display the modified string. It will just display the original entry by the user.
EDIT:
Thanks to the users below, the code is fixed, and I added a notice to the user with converted text. Thanks guys!
A # outside a string starts a comment, so #{user_input} is ignored, i.e.
print #{user_input}
is equivalent to
print
You might wonder why a single print outputs the original input. This is because without arguments print will print $_. That's a global variable which is set by gets:
user_input = gets.chomp # assume we enter "foo"
user_input #=> "foo"
$_ #=> "foo\n"
Everything works as expected if you pass a string literal:
print "#{user_input}"
or simply
print user_input
Note that gsub! returns nil if no substitutions were performed, so you can actually use it in your if statement:
if user_input.gsub!(/s/, "th")
print user_input
else
puts "I couldn't find any 's' in your entry. Please try again."
end
You just need to add double quotes around the string interpolation. Otherwise your code was just returning the input.
puts "What would you like to convert to Daffy Duck language?"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
print "#{user_input}"
else
puts "I couldn't find any 's' in your entry. Please try again."
end
You don't even need interpolation, actually. print user_input works. Notice how StackOverflow was even syntax highlighting your code as a comment. :)

setting variables equal to 'gets' in Ruby

I want to write a program that asks for two strings from the user and searches for one within the other, but I'm having some trouble making it work. The following returns "not" even when the given character is present within the string. Does anyone know what I'm doing wrong?
puts 'Enter the string that you would like to search'
content = gets
puts 'What character would you like to find?'
query = gets
if content.include? query
puts "here"
else
puts "not"
end
gets returns the string from the user including the newline character '\n' at the end. If the user enters "Hello world" and "Hello", then the strings really are:
"Hello World\n"
"Hello\n"
That makes it obvious, why your code does not find a match.
Use chomp to remove that newline characters from the end of the string.
puts 'Enter the string that you would like to search'
content = gets.chomp
puts 'What character would you like to find?'
query = gets.chomp
if content.include?(query)
puts "here"
else
puts "not"
end

Sinatra does not support multiple lines?

For the following code, why does only "World" gets printed
get '/' do
"Hello"
"World"
end
This has nothing to do with sinatra itself. It just uses the return value of the block and in ruby the return value is the last evaluated expression, which in your case is "World". This might work for you:
get '/' do
r = "Hello"
r += "World"
end
In this case you add as many string values to r as you want and the last expression would return the complete string "HelloWorld".
Correct me if I'm wrong, but I do believe in plain ruby, the last line evaluated is what gets returned.
Tomas correctly answered your question, but one way to do what I think you're meaning to do (output multiple lines), you could use:
get '/' do
output =<<EOS
Hello
World
EOS
output
end
You could use a line break char to separate lines..
get '/' do
"Hello\nWorld"
end
Don't confuse your controller with your view.
What you're probably looking for is this:
get '/' do
haml :hello_world
end
And then in views/hello_world.haml:
Hello
World
I agree with Matt.
If you want you can use that method with one file too.
get '/' do
erb :hello_world
end
__END__
##hello_world
hello
world
I just use puts inside my controller to get some debug printed to STDOUT.

Resources