rake任务中的def块

问题描述 投票:39回答:2

我得到了undefined local variable or method 'address_geo' for main:Object以下rake任务。有什么问题呢?

include Geokit::Geocoders

namespace :geocode do
  desc "Geocode to get latitude, longitude and address"
  task :all => :environment do
    @spot = Spot.find(:first)
    if @spot.latitude.blank? && [email protected]?
      puts address_geo
    end

    def address_geo
      arr = []
      arr << address if @spot.address
      arr << city if @spot.city
      arr << country if @spot.country
      arr.reject{|y|y==""}.join(", ")
    end
  end
end
ruby-on-rails ruby methods rake
2个回答
97
投票

您正在rake任务中定义方法。要获得该功能,您应该在rake任务之外定义(在任务块之外)。试试这个:

include Geokit::Geocoders

namespace :geocode do
  desc "Geocode to get latitude, longitude and address"
  task :all => :environment do
    @spot = Spot.find(:first)
    if @spot.latitude.blank? && [email protected]?
      puts address_geo
    end
  end

  def address_geo
    arr = []
    arr << address if @spot.address
    arr << city if @spot.city
    arr << country if @spot.country
    arr.reject{|y|y==""}.join(", ")
  end
end

21
投票

Careful: Methods defined in rake files end up defined on the global namespace.

我建议将方法提取到模块或类中。这是因为rake文件中定义的方法最终在全局命名空间中定义。即,然后可以从任何地方调用它们,而不仅仅是在该rake文件中(即使它是命名空间!)。

这也意味着如果在两个不同的rake任务中有两个具有相同名称的方法,则其中一个将在您不知情的情况下被覆盖。非常致命。

这里有一个很好的解释:https://kevinjalbert.com/defined_methods-in-rake-tasks-you-re-gonna-have-a-bad-time/

© www.soinside.com 2019 - 2024. All rights reserved.