在 Ruby 中放置私有方法的位置?

大多数博客、教程或书籍在任何类/模块的底部都有私有方法。这是最好的练习吗?

我发现必要时使用私有方法更方便。例如:

public
def my_method
# do something
minion_method
end


private
def minion_method
# do something
end


public
def next_method
end

通过这种方式,我发现代码更具可读性,而不是不断上下滚动,这是非常恼人的。

这种方法有什么可怕的错误吗?在底部使用私有方法不仅仅是最佳实践和其他东西吗?

62517 次浏览

I think that public methods is a some kind of interface of the object, and it's logical to place them on the most prominent place i.e. in the top of file.

You don't need to put public or private above each method. I usually put all of my private methods at the bottom of my class. Also, don't have to explicitly say public as methods are public by default. For example:

class FooBar


def some_public_method
end


def another_public_method
end


private


def some_private_method
end


def another_private method
end


end

As others have already pointed out the convention is to put private methods at the bottom, under one private class. However, you should probably also know that many programers use a double indented (4 spaces instead of 2) method for this. The reason is that often times you won't see "private" in your text editor and assume they could be public. See below for an illustration:

class FooBar


def some_public_method
end


def another_public_method
end


private


def some_private_method
end


def another_private method
end


end

This method should prevent you from having to scroll up and down and will make other programmers more comfortable in your code.

I don't like having to specify public or private for each method. Putting all private methods at the bottom lets me have a single instance of "private" per file. I guess it's a matter of taste.

The best practice in my point of view is to go sequentially and declare your methods without keeping private in point of view.

At the end, you can make make any method private by just adding: private :xmethod

Example:

class Example
def xmethod
end


def ymethod
end


def zmethod
end


private :xmethod, :zmethod


end

Does this justify your question?

I'm coming from java background and I hate to have to scroll to see method type. I think it's insane that one cannot specify method visibility per method without ugliness. So I ended up putting a comment #private before each suck method and then declaring private :....

Update: In recent ruby one can write private def method_name so the above is much less relevant.

Update 2: actually see @devpuppy's answer, it has more explanation.

There's also the option to prepend private to the method definition since Ruby 2.1.

class Example
def xmethod
end


private def ymethod
end


private_class_method def self.zmethod
end
end

You can instantly see if a method is private, no matter where in the (large) file it is. And it's consistent with many other languages. But it's a bit more typing and doesn't nicely align.

Dennis had the perfect answer, that is, when using ruby >=2.1, just prefix the def with private (or protected,public)

But I believe that it's now also possible to use private as a block as in:

private begin
def foo
end
def bar
end
end


def zip
end

One style is to group methods together so that you only use private and protected once per class at most. Another style is to specify visibility right after the method definition:

class Example
def my_private_method
end
private :my_private_method


def my_public_method
end
end

As of Ruby 2.1.0 def returns the method name as a symbol, so a more streamlined style is possible:

class Example
private def my_private_method
end


def my_public_method
end


protected def my_protected_method
end


private_class_method def self.my_private_class_method
end
end

(Note that we use private_class_method for class methods -- otherwise we'd get NameError: undefined method since private expects an instance method. Even when using it as a macro like in the original example it only affects the visibility of instance methods.)

I like this inline visibility style best, as it allows you to organize methods as you wish. It decreases the risk of adding a new method in the wrong place and inadvertently making it private.

As for the class method syntax, you can handle it this way instead:

class Example
private def my_private_method
end


class << self
private def my_private_class_method
end
end
end

I generally order my methods as follows:

  1. Constructor
  2. Other public methods, in alphabetical order
  3. private, written only once
  4. Private methods, in alphabetical order

I use “go to definition” features in my editor so that this doesn’t involve much scrolling, and in any case, if the class is big enough that scrolling becomes problematic, it probably should be broken up into several classes.