+ 1
Can we compare strings in ruby.
Like if I got user input hello then I want to put if condition that answers hi and else bye.I did it but it showed an error.
4 Answers
+ 1
lndeed it is possible to compare two strings.
Your if/else block should have a string as a condition too.
For example:
inp = gets
if inp == "Hello"
puts "Hi"
else
puts "Ok."
Maybe your mistake was in putting the "=" operator instead of "==" in creating the condition.
+ 1
inp = gets .chomp
if inp == "Hello"
puts "Hi"
else
puts "Bye"
end
chomp removes the line break from the string
http://ruby-doc.org/core-2.3.1/String.html#method-i-chomp
+ 1
Anis it showed Ok only when I entered Hello also.??
+ 1
Manoj Your Answer was right but in playground it showed me a warning /r in the middle of line treated as bla bla bla. So tell me what does it mean