What is the purpose of “!” and “?” at the end of method names?

后端 未结 5 2071
生来不讨喜
生来不讨喜 2020-12-04 07:28

Sometimes I see methods in Ruby that have \"?\" and \"!\" at the end of them, e.g:

name = \"sample_string\"
name.reverse
name.reverse!
name.is_binary_data?
<         


        
5条回答
  •  死守一世寂寞
    2020-12-04 08:07

    Question mark indicates that the method returns boolean. Already answered here:

    What does the question mark operator mean in Ruby?

    The bang indicates that the method acts on the object itself. Already answered here:

    Why are exclamation marks used in Ruby methods?

提交回复
热议问题