ruby-on-rails - 从嵌套类中调用 Ruby 模块方法

标签 ruby-on-rails ruby

除了从嵌套类中调用 Module 方法之外,还有其他方法吗?代码:

module GroupSweeper

  def expire_cache(paths)
    paths.each do |path|
      expire_page(path)
    end
  end

  class SweeperOne < ActionController::Caching::Sweeper
    include GroupSweeper
    observe Subject
    def after_save(subject)
      expire_cache([root_path,subjects_path])
    end
    def after_destroy(subject)
      expire_cache([root_path,subjects_path])
    end 
  end

end

如何从 SweeperOne 中调用 GroupSweeper 的 expire_cache 方法而不显式包含它?

感谢任何输入。

最佳答案

这里有一些循环依赖。

  • GroupSweeper 定义了一个嵌套类 SweeperOne 的
  • SweeperOne 包括 扫群

那行不通。

回答您的 ruby​​ 方法/嵌套类问题:

module MyModule
  def my_method
    puts "yo yo yo"
  end

  class MySweetClass
    def sweet_method
      puts "swweeeeeeeeeeeet"
    end
  end
end

如果你想调用 MySweetClass 的 sweet_method,你会变成:

module MyModule
  def my_method
    puts "yo yo yo"
    MySweetClass.new.sweet_method
  end

  class MySweetClass
    def sweet_method
      puts "swweeeeeeeeeeeet"
    end
  end
end

#....

class MyClass
  include MyModule
end

MyClass.new.my_method

但是!我认为您在 rails 清扫器方面走错了路。这个答案非常有策略性,但我认为你应该提出一个问题,关于你想对 rails 清扫器做什么。

关于ruby-on-rails - 从嵌套类中调用 Ruby 模块方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5326389/

相关文章:

ruby-on-rails - 如何在 rails 上更新 oracle clob 列 ruby​​,错误 : string literal too long

mysql - 无法启动 Rails 服务器 (MySQL Windows)

css - Sass::SyntaxError on Rails 生产预编译

c - 分析由 Ruby 程序调用的 C 共享库

ruby - 在 linux 和 windows 中独立分发 ruby​​ 应用程序

ruby - ruby 集合/可枚举的炫酷技巧和富有表现力的片段

ruby-on-rails - Ruby on Rails 堆栈级别太深

ruby-on-rails - 尝试使用新主题构建或提供服务时出现 Git 相关错误

javascript - jquery 禁用表单和链接中的提交/单击按钮

ruby - 如何检测 ruby​​ 中的变量更改/分配