Reputation: 13
I'm super beginner to ruby, but for some reason my if statement isn't working. Whenever the name 'Cristina' is entered, the program continues to print "Hello there".
def Cus_free_makers_eg1heChallenge(str)
str = gets
if str == "Cristina"
print "Hello Cristina!"
else
print "Hello there!"
end
return str
end
Upvotes: 1
Views: 106
Reputation: 151
This works:
str = gets.chomp
if str == "Cristina"
print "Hello Christina!"
else
print "Hello there!"
end
str
Ruby gets
statement is usually ended with chomp
or chomp!
to -- you guessed it -- "chomp" aka remove the trailing newline and carriage characters. More info in Ruby doc: https://docs.ruby-lang.org/en/3.0.0/String.html#method-i-chomp
I also took the opportunity to remove return
and also the trailing end
as both aren't necessary.
Upvotes: 1
Reputation: 12347
Add strip
to remove newline:
str = gets.strip
if str == "Cristina"
print "Hello Cristina!"
else
print "Hello there!"
end
Upvotes: 1