Why are exclamation marks used in Ruby methods?

后端 未结 10 2160
小蘑菇
小蘑菇 2020-11-22 06:01

In Ruby some methods have a question mark (?) that ask a question like include? that ask if the object in question is included, this then returns a

10条回答
  •  攒了一身酷
    2020-11-22 06:26

    Called "Destructive Methods" They tend to change the original copy of the object you are referring to.

    numbers=[1,0,10,5,8]
    numbers.collect{|n| puts n*2} # would multiply each number by two
    numbers #returns the same original copy
    numbers.collect!{|n| puts n*2} # would multiply each number by two and destructs the original copy from the array
    numbers   # returns [nil,nil,nil,nil,nil]
    

提交回复
热议问题