5 ms·
yes this makes sense because ruby is a message passing language. even accessors are messages. only self can receive private messages. and the sharing a private
by LesZedCB 3y ago
yes this makes sense because ruby is a message passing language. even accessors are messages. only self can receive private messages.
and the sharing a private instance variable between separate instances example is exactly what protected does, which ruby also has.
- caseyohara 3y agoExactly. And prior to Ruby 2.7, even calling a private method with an explicit `self` as the receiver was not allowed, only the implicit `self` from "within" the class body ("within" in quotes because, as the blog post points out, Ruby's concept of being inside a class body is unlike many other languages).
- jez 3y agoI don't think this has to be the case! I think you could build a different language with the same message passing semantics as Ruby, but with the limitations that classes are not allowed to be re-opened and method definitions must appear syntactically at the top-level of a class (and maybe a few other limitations), and then build something more similar to how `private` works in a traditional, statically-typed language. You definitely don't arrive at a language that has all the same feeling of Ruby, but you could still have the "everything is an object, and everything is message passing" character to the language.