liveBook · Manning

Additive change

As explained in The Well-Grounded Rubyist, Fourth Edition

An additive change is a modification that extends an existing Ruby class or module by adding functionality without replacing existing methods.

Suppose strings need a `pluralize` method. An additive change puts that new method on the existing `String` class rather than replacing a method Ruby already provides. Active Support’s core extensions offer a ready-made example: section 13.2.2

>> require 'active_support'
=> true
>> require 'active_support/core_ext'
=> true
>> "person".pluralize
=> "people"
>> "little_dorritt".titleize
=> "Little Dorritt"

After loading Active Support’s core extensions, strings respond to `pluralize` and `titleize`. section 13.2.2

Adding a method leaves existing methods intact, but it does not reserve the new name. If two loaded libraries add the same method name with different behavior, one definition can clobber the other. Widespread use may make a particular extension less surprising, but it cannot eliminate that risk. Be circumspect about adding methods to core classes merely because a widely used library does it. section 13.2.2

The distinction is whether an existing method changes. A pass-through override calls the old method while adding behavior; it is not a purely additive change. Refinements offer another way to limit where changes to classes take effect. section 13.2.3 section 13.2.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines the technique, illustrates it with String methods, and examines naming collisions without offering a collision-proof strategy.
Examples
Demonstrates Active Support’s `String#pluralize` and `String#titleize` using `person` and `little_dorritt`.
What it assumes
Assumes familiarity with Ruby classes and method calls; shows the `require` statements needed for the Active Support example.
What you can do after
Evaluate/understand additive core extensions and their method-name collision risk before using or writing them.
How it connects
Contrasts core extensions with pass-through overrides and relates class changes to refinements.

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.