Ruby gets problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Ruby gets problem

When I use gets in ruby it says split multiple inputs into sperate lines but when I seperate it into lines whenever I print thoose inputs it makes a new line in the output

24th Jul 2017, 3:04 PM
Muhammed Faik Ekiciler
Muhammed Faik Ekiciler - avatar
4 Answers
+ 12
x = gets y = gets => you are getting strings. x = gets.to_i y = gets.to_i => you are getting integers.
24th Jul 2017, 4:09 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 1
Could you link your code? It doesn't appear the one you have posted has the problem.
24th Jul 2017, 3:12 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
gets takes the input as a string and also includes the newline, so if you enter "5", what you end up with is "5\n" To drop newlines you use gets.chomp I'm guessing you don't want it to be a string but rather a number so you have to convert the string to a number when you recieve the input. You can do this with the .to_i method so x = gets becomes x = gets.to_i no need to use chomp as well because the newline is ignored in the conversion anyway.
24th Jul 2017, 4:07 PM
Dennis
Dennis - avatar
+ 1
thx to yall that answered my question
24th Jul 2017, 5:58 PM
Muhammed Faik Ekiciler
Muhammed Faik Ekiciler - avatar