1
如果我做`gets.chomp.downcase!`得到一個空字符串錯誤
user_input = gets.chomp
user_input.downcase!
我可以輸入沒有錯誤 一個字符串但是,如果我這樣做
user_input = gets.chomp.downcase!
我user_input
串未填充,並且字符串的任何後續工作都會返回錯誤,指出我的字符串爲空。這是爲什麼?
如果我做`gets.chomp.downcase!`得到一個空字符串錯誤
user_input = gets.chomp
user_input.downcase!
我可以輸入沒有錯誤 一個字符串但是,如果我這樣做
user_input = gets.chomp.downcase!
我user_input
串未填充,並且字符串的任何後續工作都會返回錯誤,指出我的字符串爲空。這是爲什麼?
String#downcase!
返回nil
如果沒有更改。
'hello'.downcase!
# => nil
'hello'.downcase
# => "hello"
因此,如果輸入的字符串沒有大寫字母,gets.chomp.downcase!
回報nil
。
哦哇,這是整潔。另外,感謝您編輯我的問題 –