耙如何修复未定义的局部变量或方法错误?

时间:2017-02-23 07:36:50

标签: ruby-on-rails ruby compiler-errors rake rake-task

我有一个rake任务如下 -

 desc 'send fetch request'
  task send_fetch_request: :environment do
    FacebookCrawl.new.process
  end

昨天这个任务有效,但我不知道为什么它今天不起作用。 我试图使用以下命令执行此操作 -

rake send_fetch_request

课程详情:

  class FacebookCrawl

    def initialize                   
        fb_config = YAML.load_file(Rails.root.join("config/facebook_catalog.yml"))
        @access_token = fb_config["facebook"]["access_token"]
        @product_feed_ids = fb_config["facebook"]["product_feed_ids"]
    end

    def process
        @product_feed_ids.each do |key,value|
            feed_id  = value["id"]
            feed_url = value["feed_url"]
            make_request(feed_id,feed_url,@access_token)
        end
    end 
end

我收到以下错误:

rake send_fetch_request --trace
    ** Invoke send_fetch_request (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Execute send_fetch_request
    rake aborted!
    NameError: undefined local variable or method `  FacebookCrawl' for main:Object
    /Users/raj.sharma/Documents/Developer/Feed/lib/tasks/facebook_fetch_request_task.rake:3:in `block in <top (required)>'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `call'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `block in execute'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `each'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `execute'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:194:in `block in invoke_with_call_chain'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:187:in `invoke_with_call_chain'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:152:in `invoke_task'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `each'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block in top_level'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:117:in `run_with_threads'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:102:in `top_level'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:80:in `block in run'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:77:in `run'
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
    /Users/raj.sharma/.rbenv/versions/2.2.2/bin/rake:22:in `load'
    /Users/raj.sharma/.rbenv/versions/2.2.2/bin/rake:22:in `<main>'
    Tasks: TOP => send_fetch_request

昨天它工作正常,我不知道为什么rake今天抱怨。请帮忙。

1 个答案:

答案 0 :(得分:4)

请仔细查看错误消息:

NameError: undefined local variable or method `  FacebookCrawl' for main:Object
#                    ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑  ↑↑↑

第一个赠品是,您为未定义的局部变量或方法获得NameError,而不是常量。第二个赠品是Ruby抱怨的名称:它没有抱怨FacebookCrawl,它抱怨  FacebookCrawl

在Ruby中,只有Unicode字符 U + 0020 SPACE U + 0009 CHARACTER TABULATION 被视为空格。您有两个中文 U + 3000 IDEOGRAPHIC SPACE 字符,Ruby只是将其视为名称的一部分,因为它们不是大写字符,所以它将名称视为本地变量或消息发送。

您可能应该在编辑器中打开可见的空白,例如这就是您的代码在我的编辑器中的外观,使问题显而易见:text editor with visible spaces showing the offending characters in different highlighting