Define #initialize as a private method

Hi, all and cheers to the community!

I have a question about class design. In many-many sources I saw
something like this:

class Ship
def initialize
end
end

Nothing special, but the one thing. If we introspect #initialize method
itself, we will see it’s private:

Ship.private_methods.include?(:initialize) # => true

Thus, Ship#initialize is already private even if we don’t use private
keyword at all.

So, my next idea - put #initialize method under the private keyword:

class Ship
private

def initialize
end
end

Why do this? Because (IMO) we get more clear class definition:

class Ship

API

private

Implementation

end

I use this definition about a few months. At the start it was a little
weird, but now I find it very helpful, cause when I looking into the
class, I immediately see API and only API. Constructor method
#initialize goes right after private keyword and I know where to find it
if need.

So, what do you think about this approach?

Thanks for answers.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs