Why are exclamation marks used in Ruby methods?

后端 未结 10 2076
小蘑菇
小蘑菇 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:04

    !
    

    I like to think of this as an explosive change that destroys all that has gone before it. Bang or exclamation mark means that you are making a permanent saved change in your code.

    If you use for example Ruby's method for global substitutiongsub!the substitution you make is permanent.

    Another way you can imagine it, is opening a text file and doing find and replace, followed by saving. ! does the same in your code.

    Another useful reminder if you come from the bash world is sed -i has this similar effect of making permanent saved change.

提交回复
热议问题