在此代碼段: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
的返回值?
在此代碼段: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
的返回值?
就像在所有其他情況下使用chomp
一樣。如果您從gets
獲得字符串,則會附帶拖尾的新行字符。 chomp
將其刪除。
在您的上述例子
gets
基本上從stdin獲取用戶輸入,並返回其被傳遞到chomp
chomp
返回與從str的端去除所述給定記錄分隔一個新的字符串的字符串(如果當下)。
這個字符串不會傳遞給chomp,但chomp會在這個字符串上被調用。 – 2014-11-25 15:11:26
使用谷歌的這樣的問題。 「ruby chomp」會直接導致你的文檔 – 2014-11-25 09:16:01