5 ms·
The Ruby example actually involves generating the getter and setter at runtime via attr_accessor. In contrast to Python, in Ruby all instance member variables
by msbarnett 15y ago
The Ruby example actually involves generating the getter and setter at runtime via attr_accessor.
In contrast to Python, in Ruby all instance member variables are private.
- Udo 15y agoYes, but functionally the Ruby example is the equivalent of a public member variable. Since those are public by default in PHP, there is no need to complicate the code by first making it private and then implementing a getter and a setter.
- chc 15y agoThis is a kind of a semantic game. You might say they're "equivalent," but you're still doing different things — and different is very rarely truly equal. For example, the Ruby accessors could be hooked or altered later without changing the interface. Not so if you use a public variable (which Ruby doesn't have, for this very reason). The only language I know of where public variables and accessors are truly interchangeable is Scala.
- Udo 15y agoEverything is a semantics game: meaning matters. This is not about playing "gotcha" with trivialities. I'd like to convince you that there is a significant difference or bias about the way those languages are being showcased. Isn't the whole point of this table to compare equivalent snippets of code? You're right, those languages do things very differently from one another - especially when it comes to OO. In this case, the stated goal was to define a class that exposes a public property. In this I believe PHP and probably Perl were represented badly. If you don't believe me, look at the Python example: it's the exact same implementation I translated into PHP syntax above. So my terse implementation is an exact match for the Python example and it's a functional match for the Ruby example. Hence, I can't help but think that PHP and Perl were misrepresented by making them jump through unnecessary hoops that do nothing but make the language look more complicated.
- grncdr 15y agoI agree with your general point, but feel I should point out that this is possible in python using properties: class Int(object): def __init__(self, value): self.value = value @property def value(self): print("Accessing value") return self.value @value.setter def value(self, value): print("Setting value") self.value = value