2015-11-08 35 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

哦哇,这是整洁。另外,感谢您编辑我的问题 –