Ruby 简单测验中的输出问题

Ruby 简单测验中的输出问题,ruby,Ruby,在下面的代码和控制台输出中,似乎一切都很好,但最后输出的格式看起来很糟糕;每次调用一个值时,它都会在后面换行 代码: puts "Please enter your name: " name = gets puts "Please enter your age: " age = gets puts "Please enter the name of your favourite movie: " movie = gets puts "Please rate this movie out of 1

在下面的代码和控制台输出中,似乎一切都很好,但最后输出的格式看起来很糟糕;每次调用一个值时,它都会在后面换行

代码:

puts "Please enter your name: "
name = gets
puts "Please enter your age: "
age = gets
puts "Please enter the name of your favourite movie: "
movie = gets
puts "Please rate this movie out of 10: "
movieR = gets

puts "Just to confirm; your name is #{name}.\nYou are #{age} years old.\nYour favourite movie is #{movie}, which you rate as #{movieR}/10"
Please enter your name:                                                                                                                                                                 
Rob                                                                                                                                                                                     
Please enter your age:                                                                                                                                                                  
26                                                                                                                                                                                      
Please enter the name of your favourite movie:                                                                                                                                          
Jurassic Park                                                                                                                                                                           
Please rate this movie out of 10:                                                                                                                                                       
10                                                                                                                                                                                      
Just to confirm; your name is Rob                                                                                                                                                       
.                                                                                                                                                                                       
You are 26                                                                                                                                                                              
 years old.                                                                                                                                                                             
Your favourite movie is Jurassic Park                                                                                                                                                   
, which you rate as 10                                                                                                                                                                  
/10
控制台输出:

puts "Please enter your name: "
name = gets
puts "Please enter your age: "
age = gets
puts "Please enter the name of your favourite movie: "
movie = gets
puts "Please rate this movie out of 10: "
movieR = gets

puts "Just to confirm; your name is #{name}.\nYou are #{age} years old.\nYour favourite movie is #{movie}, which you rate as #{movieR}/10"
Please enter your name:                                                                                                                                                                 
Rob                                                                                                                                                                                     
Please enter your age:                                                                                                                                                                  
26                                                                                                                                                                                      
Please enter the name of your favourite movie:                                                                                                                                          
Jurassic Park                                                                                                                                                                           
Please rate this movie out of 10:                                                                                                                                                       
10                                                                                                                                                                                      
Just to confirm; your name is Rob                                                                                                                                                       
.                                                                                                                                                                                       
You are 26                                                                                                                                                                              
 years old.                                                                                                                                                                             
Your favourite movie is Jurassic Park                                                                                                                                                   
, which you rate as 10                                                                                                                                                                  
/10

这是由于
获取
使用\n(换行符)作为输入的默认分隔符造成的,除非提供了另一个分隔符(此处未提供),因此当显示结果时,换行符将作为值的一部分包含在内;请改用
gets.chomp

这是由于
gets
使用\n(换行符)作为输入的默认分隔符造成的,除非提供了另一个分隔符(此处未提供),因此在显示结果时,换行符将作为值的一部分包含在内;使用
gets.chomp
代替。

谢谢您的编辑spickermann,我确实想知道是否应该让代码位更清晰。在这个答案的换行部分添加GET有点模糊。因为
获取
没有附加内容<代码>获取从I/O流读取数据,直到找到分隔符。如果不提供特定的分隔符,则使用通常为
\n
(换行符)的
$/
。分隔符包含在
get
返回的值中。谢谢!因此,从我先前在控制台中输入的变量中有效地“chomps”回车“\n”键?我甚至没有意识到它会包含这些内容。@Rob Yep,chomp方法几乎只是返回输入,而没有终止换行符。很多人没有意识到GET是这样工作的,别担心(包括我刚开始使用Ruby的时候)谢谢编辑spickermann,我确实想知道我是否应该让代码更清晰。这个答案中的GET加上换行部分有点模糊。因为
获取
没有附加内容<代码>获取从I/O流读取数据,直到找到分隔符。如果不提供特定的分隔符,则使用通常为
\n
(换行符)的
$/
。分隔符包含在
get
返回的值中。谢谢!因此,从我先前在控制台中输入的变量中有效地“chomps”回车“\n”键?我甚至没有意识到它会包含这些内容。@Rob Yep,chomp方法几乎只是返回输入,而没有终止换行符。很多人没有意识到这样做是可行的,别担心(包括我刚开始使用Ruby时)