如何在Rails模块中定义辅助方法?

8 methods module helper ruby-on-rails-4

我正在使用Rails 4.2.4.如何在模块中定义辅助(私有)方法?我有这个模块

module WebpageHelper

  def get_url(url)
    content = get_content(url)
    ..
  end

  def get_content(url)
    …
  end

  module_function :get_url

end
Run Code Online (Sandbox Code Playgroud)

我不希望方法"get_content"可以公开访问,但是使用上面的代码我得到了错误

Error during processing: undefined method `get_content' for WebpageHelper:Module
Run Code Online (Sandbox Code Playgroud)

如何在我的模块中正确定义私有帮助器方法?

小智 0

我认为最好的方法(主要是现有库的编写方式)是通过在模块内创建一个处理所有逻辑的类来实现这一点,并且该模块只是提供了一种方便的方法。

文档在这里;

私有方法

或者参见这里一些不错的例子,例如class << self选项。