Why do some ruby ​​methods need a beat, while others cannot be a destructive method?

For example, array.pop does not need a hit to constantly modify the array. Why is this so and what was the reason for developing these specific Ruby methods without this correspondence?

+8
methods ruby
source share
4 answers

Hacking methods are most often used to distinguish between a dangerous and a safe version of the same method. Here are a few examples that could be distinguished from the bang / no-bang combination:

  • mutator methods - one version modifies the object, the other returns a copy and leaves the original object unchanged
  • If an error occurs, one version throws an exception and the other writes an error message to the log or does nothing

However, the convention is to leave the bag if there is only one version that makes sense. For example, a pop array without actual change does not make sense. In this case, it will be another operation: Array#last . Many methods modify the object on which they are called, such as setters. We also do not need to write them in error, because it is clear that they are changing the object.

Finally, there are a few exceptions to this when some developers may use the bang method without implementing a fuzzy copy. In these cases, a bag is simply used as a way to visualize method calls. For example:

  • the method does something dangerous or destructive
  • the method does something unexpected
  • The method has a significant impact on performance.
+12
source share

The brand is used to distinguish between a dangerous and less dangerous version of the same method. There is only one pop method, so there is nothing to distinguish.

Note: the name of the method has absolutely nothing to do with what it does. Whether the method is destructive or not depends on what code it executes, and not on what name it has.

+2
source share

Suffix ! means the method is a dangerous version of another method. For example, save! is a dangerous version of save . Dangerous can mean in-place editing, something to do with more severe errors, etc. No need to use a suffix ! for a dangerous method, but does not need a safer counterpart. It’s also just a naming convention, so Ruby doesn’t limit what you can and cannot do if the method executes or doesn't end ! .

There is a common misconception that every method that edits something in place must end with ! . This is wrong ! only required when a more dangerous version of an existing method exists, and this does not necessarily mean that the dangerous method is edited in place. For example, in Rails ActiveRecord::Base#save! is a version of ActiveRecord::Base#save that performs checks.

+1
source share

The meaning of the explosion in Ruby is “caution”. This means that you should use the method with caution, nothing more. I can’t find the link anymore, but people in the government explicitly stated that it was a destructive method. An explosion is just a semantic element associated with caution. The programmer must weigh everything and decide when to use the punch.

For example, in my simulator, I use the #step method to get the step size.

 simulation.step #=> 0.42 

and step! to actually complete the simulation phase.

 simulation.step! #=> takes the simulation to the next time step 

But as for the #reset method, I decided that the word "reset" is quite a lot, and there is no need to use bang to warn the user that the simulation state will be destroyed:

 simulation.reset #=> resets the simulation back to the initial state 

PS: Now I remember once, Matz half-jokingly said that he regrets that he introduces methods with kick in Ruby in general, because the kick is semantically so ambiguous.

+1
source share

All Articles