Ash*_*Ash 11 ruby naming-conventions
我只是学习Ruby而且我不太了解有和没有'!'的几个Ruby方法之间的区别.在末尾.有什么不同?为什么我会使用一个而不是另一个?
小智 12
非bang downcase()方法只返回一个表示字符串向下的新对象.
爆炸版本修改你的字符串本身.
my_text = "MY TEXT"
my_new_text = my_text.downcase
puts my_new_text # will print out "my text"
puts my_text # will print out "MY TEXT" - the non-bang method doesn't touch it
my_text.downcase!
puts my_text # will print out "my text". The bang version has modified the object you're calling the method on
Run Code Online (Sandbox Code Playgroud)