【问题标题】:Method calls in one rake file task being handled by methods of same name in another rake file一个 rake 文件任务中的方法调用由另一个 rake 文件中的同名方法处理
【发布时间】:2014-01-13 15:51:50
【问题描述】:

使用 Rails 2.3.*

假设我在两个 rake 文件 A.rake 和 B.rake 中有一个名为 some_method() 的方法。 我发现如果我在 B.rake 中调用 some_method(),A.rake 中的方法就是实际被调用的方法。

那么,在 rake 文件中定义对于该文件中定义的 rake 任务而言“本地”的辅助方法的最佳方法是什么?

谢谢

【问题讨论】:

  • 示例代码会有所帮助...

标签: ruby-on-rails rake


【解决方案1】:

您可以在任务中定义您的助手,以使其可用于该任务和所有后续任务:

desc 'has access to local helper'
task :accessible do
  def helper
    return "the helper"
  end

  puts "I have access to #{helper}"
end

desc 'has access too'
task 'after-accessible' => ['accessible'] do
  puts "this ran after 'accessible' but still has access to '#{helper}"
end

desc 'does not have access to the helper'
task :outside do
  puts helper # fails if runs before :accessible
end

也许最好的办法是重构您的 Rakefile 和帮助程序代码,这样两个 Rakefile 就不会相互加载。

【讨论】:

    猜你喜欢
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 2012-12-28
    • 2013-07-20
    相关资源
    最近更新 更多