Ruby “.downcase! ” and “downcase” confusion

后端 未结 4 1941
耶瑟儿~
耶瑟儿~ 2021-01-17 06:10

I just started Ruby programming. I had read Difference Between downcase and downcase! in Ruby. However I encounter an interesting problem in practice, here is code:

相关标签:
4条回答
  • 2021-01-17 06:47

    a.downcase! modifies "a" directly -- you do not need to assign the result to "a".

    1.9.3p362 :003 > a = "A"
     => "A" 
    1.9.3p362 :004 > a.downcase!
     => "a" 
    1.9.3p362 :005 > puts a
    a
     => nil 
    1.9.3p362 :006 > a = "A"
     => "A" 
    1.9.3p362 :007 > a.downcase
     => "a" 
    1.9.3p362 :008 > puts a
    A
     => nil 
    

    The danger of assigning variable = variable.downcase! is that if variable is already downcase then you have just set the variable to nil -- probably not your intent.

    1.9.3p362 :001 > variable = 'a'                                                                                                                                                                                                                                               
     => "a"                                                                                                                                                                                                                                                                       
    1.9.3p362 :002 > variable = variable.downcase!
     => nil                                                                                                                                                                                                                                                                       
    1.9.3p362 :003 > puts variable                                                                                                                                                                                                                                                
    
     => nil 
    
    0 讨论(0)
  • 2021-01-17 06:49

    downcase! is a method which modifies the string in-place (whereas downcase creates a new string instance).

    The return value of downcase! is nil if the string has not been modified, or the new modified string. In the latter case the string in a gets overwritten. The correct way of using downcase! is:

    a = "LOWER"
    a.downcase! # no assignment to a here
    print a # prints "lower", the original "LOWER" is lost
    

    And for downcase:

    a = "LOWER"
    print a.downcase # a is still "LOWER", but "lower" gets printed
    

    As a general rule of thumb: If a methods ends with !, the method overwrites values or modifies state in your variables.

    Additionally in your case:

    print "lower".downcase! # prints nil, because "lower" is already written in lower case
    
    0 讨论(0)
  • 2021-01-17 07:01

    downcase! will return nil if no changes were made. You can check it with ri downcase!

    0 讨论(0)
  • 2021-01-17 07:08

    downcase! → str or nil

    Downcases the contents of str, returning nil if no changes were made. Note: case replacement is effective only in ASCII region.

    Documentation: String#downcase!

    0 讨论(0)
提交回复
热议问题