在 Ruby 中将私有方法放在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10724221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:08:49  来源:igfitidea点击:

Where to place private methods in Ruby?

rubyconventions

提问by ZX12R

Most of the blogs or tutorials or books have private methods at the bottom of any class/module. Is this the best practice?

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

I find having private methods as and when necessary more convenient. For example:

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

public
def my_method
  # do something
  minion_method
end

private
def minion_method
  # do something
end

public
def next_method
end

This way I find the code more readable instead of scrolling up and down continuously which is very irritating.

这样我发现代码更具可读性,而不是连续上下滚动,这很烦人。

Is there something terribly wrong in this approach? Is having private methods at the bottom not just a best practice and something else?

这种方法有什么可怕的错误吗?在底部拥有私有方法不仅是最佳实践还是其他什么?

回答by kiddorails

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

最后,您只需添加以下内容即可将任何方法设为私有: private :xmethod

Example:

例子:

class Example
 def xmethod
 end

 def ymethod
 end

 def zmethod 
 end

 private :xmethod, :zmethod

end

Does this justify your question?

这是否证明了您的问题?

回答by Dennis

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

private从 Ruby 2.1 开始,还有一个选项可以添加到方法定义之前。

class Example

 def xmethod
 end

 private def ymethod
 end

 private def zmethod 
 end

end

Looking at the definition, you instantly know if a method is private, no matter where in the file it's defined. It's a bit more typing (if you don't autocomplete) and not all your defs will be nicely aligned.

查看定义,您立即知道一个方法是否是私有的,无论它在文件中的哪个位置定义。打字有点多(如果你不自动完成),并不是所有的defs 都会很好地对齐。

回答by Noah Clark

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:

正如其他人已经指出的那样,惯例是将私有方法放在底部,在一个私有类下。但是,您可能还应该知道,许多程序员为此使用双缩进(4 个空格而不是 2 个空格)方法。原因是很多时候您不会在文本编辑器中看到“私有”并假设它们可能是公开的。请参阅下面的说明:

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.

这种方法应该可以防止您不得不上下滚动,并使其他程序员更熟悉您的代码。

回答by Flexoid

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.

我认为公共方法是对象的某种接口,将它们放在最显眼的地方,即文件顶部,是合乎逻辑的。

回答by Kyle Decot

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

你不需要把publicprivate放在每个方法之上。我通常把我所有的私有方法放在我班级的底部。此外,不必明确说明public方法默认是公开的。例如:

class FooBar

  def some_public_method
  end

  def another_public_method
  end

private

  def some_private_method
  end

  def another_private method
  end

end

回答by akostadinov

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 #privatebefore each suck method and then declaring private :....

我来自 java 背景,我讨厌必须滚动才能查看方法类型。我认为人们不能在没有丑陋的情况下指定每个方法的方法可见性是很疯狂的。所以我最终#private在每个糟糕的方法之前放了一条评论,然后声明private :....

回答by David

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.

我不喜欢必须为每个方法指定 public 或 private。将所有私有方法放在底部让我每个文件都有一个“私有”实例。我想这是一个品味问题。

回答by devpuppy

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

一种风格是将方法组合在一起,这样每个类最多只能使用privateprotected一次。另一种风格是在方法定义之后立即指定可见性:

class Example
  def my_private_method
  end
  private :my_private_method

  def my_public_method
  end
end

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

从 Ruby 2.1.0 开始,def将方法名称作为符号返回,因此可以使用更精简的样式:

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_methodfor class methods -- otherwise we'd get NameError: undefined methodsince privateexpects an instance method. Even when using it as a macro like in the original example it only affects the visibility of instance methods.)

(请注意,我们使用private_class_method类方法-否则我们会得到NameError: undefined method,因为private预期的实例方法使用它,即使作为一个宏像原来的例子,只会影响的实例方法的可见性。)

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

回答by edx

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

Dennis 给出了完美的答案,即当使用 ruby​​ >=2.1 时,只需在 def 前加上 private(或 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

回答by Marnen Laibow-Koser

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
  1. 构造函数
  2. 其他公共方法,按字母顺序
  3. private,只写一次
  4. 私有方法,按字母顺序

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.

我在我的编辑器中使用了“转到定义”功能,这样就不会涉及太多的滚动,而且在任何情况下,如果类足够大以至于滚动变得有问题,那么它可能应该分解为几个类。