Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby chomp有什么用?_Ruby - Fatal编程技术网

Ruby chomp有什么用?

Ruby chomp有什么用?,ruby,Ruby,在此代码段中: puts "You enter a dark room with two doors. Do you go through door #1 or door #2?" print "> " door = $stdin.gets.chomp 对get for的返回值的chomp调用是什么?就像在其他情况下使用chomp一样。若您从gets获取字符串,它将带有尾随的新行字符。chomp将其移除 在上面的示例中 从stdin获取用户输入,并返回一个字符串,该字符串将传递给ch

在此代码段中:

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

print "> "
door = $stdin.gets.chomp
对get for的返回值的chomp调用是什么?

就像在其他情况下使用chomp一样。若您从gets获取字符串,它将带有尾随的新行字符。chomp将其移除

在上面的示例中

从stdin获取用户输入,并返回一个字符串,该字符串将传递给chomp


chomp返回一个新字符串,如果存在,则从str的末尾删除给定的记录分隔符。

使用google解决此类问题。ruby chomp将直接引导您访问Documentation此字符串不会传递给chomp,但会在此字符串上调用chomp。