+ 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.

7th Nov 2016, 2:11 PM
Apurav Mahajan
Apurav Mahajan - avatar
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.
7th Nov 2016, 8:24 PM
Anis Djellali
Anis Djellali - avatar
+ 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
8th Nov 2016, 3:03 AM
மனோஜ்குமார் பழனிச்சாமி
மனோஜ்குமார் பழனிச்சாமி - avatar
+ 1
Anis it showed Ok only when I entered Hello also.??
8th Nov 2016, 1:49 PM
Apurav Mahajan
Apurav Mahajan - avatar
+ 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
8th Nov 2016, 1:51 PM
Apurav Mahajan
Apurav Mahajan - avatar