2 ms·
They are. (And generated behind the scenes by "attr_accessor :sanity", if you'd prefer the short version.) The benefit everything-is-a-message is that you can
by damncabbage 13y ago
They are. (And generated behind the scenes by "attr_accessor :sanity", if you'd prefer the short version.)
The benefit everything-is-a-message is that you can swap out the implementation of sanity (or sanity=) if needed, eg.
Before:
attr_accessor :radius, :diameter
After:
# Refactored radius to be the new source of truth.
def diameter
radius * 2
end
def diameter=(val)
self.radius = val / 2
end
In PHP (for example), MyClass->diameter and MyClass->diameter() are completely different things. Any existing code (including code you don't control) needs updating to change references from the former to the latter when moving logic into something that was previously static.
This article is flawed by holding up those six lines in themselves as something great, but I think the point the author is trying to get across is valid.