4 ms·
"Dividing methods into private and public is for control freaks, you don’t need it in Ruby" Coming from a Python dev's perspective, does Ruby have the concept
by Pewpewarrows 15y ago
"Dividing methods into private and public is for control freaks, you don’t need it in Ruby"
Coming from a Python dev's perspective, does Ruby have the concept of public/private? Python just has the underscore and double underscore conventions: if you absolutely have to you can use the "private" variables and methods, but in doing so you still understand that it might change without notice in future versions. "We're all adults here" is the the motto, if I recall.
- gabebw 15y agoYes, Ruby has private methods: def i_am_public ... end def i_am_private ... end private :i_am_private private def i_am_also_private ... end
- avdi 15y agoRuby has a public/private divide which can easily be circumvented. But the circumvention is a bit ugly, which is a good thing - it makes the circumvention obvious in the code. Personally, I think Ruby hits a sweet spot here: the privacy curtain is strong enough that Ruby WILL let you know when you've stepped off the path of using methods which are part of the stable API. But you can then tell Ruby "yes, I understand, and I know what I'm doing".
- route66 15y agoStrictly speaking ruby, private only forbids calling the method in question with an explicit receiver. object_instance.private_method is obviously prohibited, but so is self.private_method when called from another class method. The latter one is fine when private_method is called, without the self. To circumvent privateness in the other case ("calling an underscored method") you would have to say object_instance.send(:private_method).
- petercooper 15y agoLuckily there's also protected, which allows you to call with an explicit receiver, but only from an instance of the same class (or within its hierarchy).