3 ms·
A tangent, but: I've read and written a lot of Ruby—except never much of the stdlib, for some reason—and I've literally never seen anyone use the `private` pseu
by derefr 6y ago
A tangent, but: I've read and written a lot of Ruby—except never much of the stdlib, for some reason—and I've literally never seen anyone use the `private` pseudo-keyword in the Java-esque way it's used here (`private def foo...` on the same line), to the point that I didn't even realize that that was possible.
I've always only seen these usages:
• `private` on its own line with surrounding whitespace, treated as a section-break in the class, where it has a "mode-switch for all that follows" effect similar to `private:` in C++.
• `private :foo` on a separate line after `def foo...`, where it's just acting as a standalone call to one of Class's "builder" DSL methods, like an `attr_reader` call.
• `private\ndef foo` (rare), as if `private` were some Java-like annotation modifying just the following method (though this is really just the first mode-switching kind, and later methods will need a `public\n` "annotation" to go back.)
Why is that? It seems startlingly obvious, once you see it done, that `private def ...` is the idiomatic way to use it. (After all, they bothered to make `def` return the symbolic name of the function you defined, just so you could chain it into class-builder-methods like this.)
- abhorrence 6y agodef returning the method name as a symbol wasn’t added until ruby 2.1, and doesn’t seem to have been compelling enough to cause people to change their ways.
- dragonwriter 6y ago> It seems startlingly obvious, once you see it done, that `private def ...` is the idiomatic way to use it. As you note that almost no one does it in Ruby, it is not the idiomatic way in Ruby. Idioms change slowly, and this wasn't supported for most of Ruby history.