2014-11-25 33 views
-5

在此代码段:chomp有什么用?

puts "You enter a dark room with two doors. Do you go through door #1 or door #2?" 

print "> " 
door = $stdin.gets.chomp 

什么chomp呼叫的gets的返回值?

+4

使用谷歌的这样的问题。 “ruby chomp”会直接导致你的文档 – 2014-11-25 09:16:01

回答

3

就像在所有其他情况下使用chomp一样。如果您从gets获得字符串,则会附带拖尾的新行字符。 chomp将其删除。

0

在您的上述例子

gets基本上从stdin获取用户输入,并返回其被传递到chomp

chomp返回与从str的端去除所述给定记录分隔一个新的字符串的字符串(如果当下)。

+0

这个字符串不会传递给chomp,但chomp会在这个字符串上被调用。 – 2014-11-25 15:11:26