liveBook · Manning

Method pair

As explained in The Well-Grounded Rubyist, Fourth Edition

A method pair is a pair of similarly named Ruby methods in which the bang and nonbang forms perform substantially the same operation but differ in side effects, return value, or other behavior.

A method pair consists of similarly named Ruby methods whose bang and nonbang forms perform substantially the same operation but differ in side effects, return value, or another behavior. The exclamation point communicates a contrast; it has no useful meaning in isolation. section 7.3.2

Use bang notation when:

  • A same-named method without ! already exists. section 7.3.2
  • Both methods do substantially the same thing. section 7.3.2
  • The bang form changes side effects, return value, or another behavior. section 7.3.2

Naming a save operation

Name a file-writing method save when it backs up the original file, and reserve save! for a paired variant that writes without making that backup. Do not add ! merely because writing a file feels dangerous. section 7.3.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains the pairing rule, behavioral differences, and naming rationale, but stops short of cataloging specific APIs.
Examples
Uses save and save! to contrast file backup behavior and warns against labeling a solitary operation dangerous.
What you can do after
Evaluate and understand when a Ruby method deserves bang notation and when a clearer unpaired name is appropriate.
How it connects
Connects bang notation with side effects, return values, receivers, and method naming conventions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.