+ 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
4 ответов
+ 12
x = gets
y = gets
=> you are getting strings.
x = gets.to_i
y = gets.to_i
=> you are getting integers.
+ 1
Could you link your code? It doesn't appear the one you have posted has the problem.
+ 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.
+ 1
thx to yall that answered my question