3 ms·
you can't assume that a property is a property or a function. In other languages, the property versus accessor distinction is more limited and less confusing.
by jamesbritt 13y ago
you can't assume that a property is a property or a function. In other languages, the property versus accessor distinction is more limited and less confusing.
Ruby does not have "properties." All data is private and you interact with objects using messages.
As a user of an object's interface all you need to know is how something behaves when sent various messages. Whether "foo = 12" is really setting an instance variable named "@foo" or something else entirely should be irrelevant. It's an implementation detail.
I realize this sounds possibly condescending and pedantic, but I've seen too many Ruby tutorials that try to present the language in terms of behavior found in other languages where the result is a broken mental model of how Ruby actually works. Hence, confusion.
Unfortunately the conflation of methods that happen to get or set instance variables with the notion of properties (AKA "attributes") has become ingrained.
method_missing, which is a bit of a hack and is at least a double edged sword, makes Ruby rock? Sure, it's handy magic, but it's a pretty dangerous form of magic.
Thinking in terms of messages sent to a receiver, "method_missing" is far less magical. It's more something you'd expect in a message-handling system.