加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-4 – 如何在Rails模块中定义辅助方法?

发布时间:2020-12-17 04:13:28 所属栏目:百科 来源:网络整理
导读:我正在使用Rails 4.2.4.如何在模块中定义辅助(私有)方法?我有这个模块 module WebpageHelper def get_url(url) content = get_content(url) .. end def get_content(url) … end module_function :get_urlend 我不希望方法“get_content”可以公开访问,但是
我正在使用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

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

Error during processing: undefined method `get_content' for WebpageHelper:Module

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

解决方法

我认为最好的方法(以及主要是如何编写现有的lib)通过在模块中创建一个处理所有逻辑的类来实现这一点,而模块只是提供了一种方便的方法.

文件在这里;

Private method

或者参见Here的一些不错的例子,例如: class<<自我选择.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读