Behaviour of Array bang methods

前端 未结 2 1066
迷失自我
迷失自我 2021-01-11 09:16

Some bang version of Array methods are like compact!, reject!, flatten!, uniq! return nil if no

2条回答
  •  终归单人心
    2021-01-11 09:58

    The bang (!) methods do modify the current object in place, but they do return nil if there are no affected elements per the documentation. This is useful if, for whatever reason, you need to do something if you did modify the array in question.

    if array.flatten!
      puts "Oh yeah... flattened that array!"
    end
    

提交回复
热议问题