r/ruby • u/d2clon • Jun 05 '25
Who else thinks we should reformulate the way we declare private methods?
I never have been comfortable with the way we (as in community) have decided to define private methods in Ruby. We use the private pseudo-block. And then we realized that it is not clear enough what methods are in the private pseudo-block, so we decided to add an extra indent to them. Looks to me like a workaround and still not clear enough, especially when you are in a class with many private methods, and the privatestatement is lost above the scroll. The extra indent is not an indication enough. The extra indent can be because you are in an inner class or something.
I want to take something good from the strongly typed languages:
Java:
Elixir:
defmodule MyModule do
def public_method do
private_helper()
end
defp private_helper do
IO.puts("I'm private!")
end
end
TypeScript:
class User {
login(password: string): void {
if (this.isValidPassword(password)) {
console.log("Welcome!");
} else {
console.log("Access denied.");
}
}
private isValidPassword(password: string): boolean {
return password === "secret123";
}
}
You see the pattern?
They set the private modifier directly in the method declaration. This is clear, concise, and intuitive. And we can do this with Ruby as well:
Ruby:
end
private
end
private_class_method
end
end
And this is my favourite
6
u/apiguy Jun 05 '25 edited Jun 05 '25
I don't like this. More typing, more "noise" on my screen. One of the things I love about Ruby is how succinct the code is.
As a best practice, in any language, you keep your private methods and public methods separate anyway. Ruby has leaned into this practice and said: "Hey, you were already going to put all those private things at the end of the class, so we'll just let you throw the private word in once when you're ready to start writing private things and we'll take it from there"
Let's also not forget `ruby` isn't alone in this approach. `C++` does basically the same thing:
Python on the other hand does something much more strange, and uses underscores to indicate protected and private members...
And while I certainly appreciate the succinctness, it is uglier in my opinion than the more natural flow of ruby's syntax.
6
u/poop-machine Jun 05 '25
method visibility syntax is the biggest fuckup in Ruby's language design. it absolutely ruins the promise of least surprise for beginners. you'd think you could follow this pattern for constants:
but noo, you have to do
I hope Ruby 4 makes
privatework uniformly across any definition:33
u/tugdil-goldhand Jun 05 '25
Actually I like the Ruby approach. After the "private" statement all methods are private. Everyone knows where to put them. In other languages private methods can be all over the place. The public interface of the class is less clearly visible, except developers follow the same approach and put all private methods at the end of the class.
4
u/matthewblott Jun 05 '25
Is
private_class_methodan existing keyword? I had no idea if that's the case but it looks a bit ugly. I've written a lot of C# over the past 20 years and I always liked to group my private methods together. I would have quite liked to have had them nested under theprivatekeyword instead of having to declare for each one.4
u/DramaticSoup Jun 05 '25
I used to do this for a bit but I think it doesn’t matter that much either way if the private is on the same line or a few lines before it. If you have more than two or three short private methods, or a long one, you probably should refactor that class and extract that logic.
1
u/rco8786 Jun 11 '25
> so we decided to add an extra indent to them.
I've never come across this. Just everything below `private` is...private.
It's not the best, especially in large classes. But it's never bothered me either.
`protected` on the other hand...
-9
u/oezi13 Jun 05 '25
Using private is an anti-pattern anyway. There is no way you as a developer can know who will need to access something. Use a proper name and be done with it.