2

我在 Ruby 2.3.1p112 和 Rails 4.2.7.1 中进行编码,并在尝试在其中一个 rake 文件中使用 if 语句时遇到了这个错误(?)。

我称之为 rake 任务:

task :bar, [:argument] => :environment do |_task, arg|
  binding.pry
  if arg.blank?
    # do stuff
  else
    # do other stuff
  end
end

来自这个工人:

 # ...
 def perform(location = nil)
    Rake::Task["foo:bar"].execute(location)
 end
 # ...

当代码到达 binding.pry 行时,我得到以下问题: 空白错误

这确实是一个错误还是我在这里缺乏一些基本知识?谢谢!

4

1 回答 1

3

你要

arg[:argument].blank?

因为arg是带:argument键的哈希。

附带说明:以下将是任务的更具描述性的定义(注意复数argslocation因为它看起来像您正在传递位置):

task :bar, [:location] => :environment do |_task, args|
  if args[:location].blank?
    # do stuff
  else
    # do other stuff
  end
end
于 2018-09-04T18:34:47.583 回答