ruby - print "Enter a character " a = gets if (a == 'b') puts "b was pressed" end -
i new learner ruby. tried
print "enter character " = gets if (a == "b") puts "b pressed" end
and well
print "enter character " = gets.to_s if (a == 'b') puts "b pressed" end
you missed method string#chomp
. change code below :
print "enter character " = gets.chomp if (a == "b") puts "b pressed" end
now run code:
kirti@kirti-aspire-5733z:~/ruby$ ruby so.rb enter character b b pressed kirti@kirti-aspire-5733z:~/ruby$
note: code not working,as a = gets
assigning "b\n"
variable a
,which of course not equal "b"
.but using #chomp
remove \n
string,you input command prompt , produce expected output.
Comments
Post a Comment