2015-11-08 17 views
1

如果我做`gets.chomp.downcase!`得到一個空字符串錯誤

user_input = gets.chomp 
user_input.downcase! 

我可以輸入沒有錯誤 一個字符串但是,如果我這樣做

user_input = gets.chomp.downcase! 

user_input串未填充,並且字符串的任何後續工作都會返回錯誤,指出我的字符串爲空。這是爲什麼?

回答

3

String#downcase!返回nil如果沒有更改。

'hello'.downcase! 
# => nil 
'hello'.downcase 
# => "hello" 

因此,如果輸入的字符串沒有大寫字母,gets.chomp.downcase!回報nil

+0

哦哇,這是整潔。另外,感謝您編輯我的問題 –