瑞克任务中的def块

瑞克任务中的def块

本文介绍了瑞克任务中的def块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了undefined local variable or method 'address_geo' for main:Object的以下瑞克任务.有什么问题吗?

I got undefined local variable or method 'address_geo' for main:Object with the following rake task. What's the problem with it?

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

推荐答案

您正在rake任务中定义方法.为了获得该功能,您应该在rake任务外部(任务块外部)进行定义.试试这个:

You are defining the method inside the rake task. For getting the function, you should define outside the rake task (outside the task block). Try this:

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

这篇关于瑞克任务中的def块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:53