gpt4 book ai didi

ruby - 无法将 args 传递给 rake 任务 - 语法是否已更改?

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:42 25 4
gpt4 key购买 nike

我正在使用具有 rake (0.9.2.2) 的 Rails 3.1.3 项目。我想在 rake 任务中这样做:像这样调用它

rake tale:import_kml /path/to/file.txt

然后在 rake 任务中,以 args.filename 访问“/path/to/file.txt”

我以为我可以这样做(puts 用于一些调试):

namespace :tale do
desc "Expects to get a file or folder name as the first argument, and passes that to Tale.import_kml"
task(:import_kml, [:filename] => :environment) do |t, args|
puts "args = #{args.inspect}"
if File.exists?(args.filename)
Tale.import_kml(filename)
end
end
end

但是,我明白了:

** Invoke tale:import_kml (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute tale:import_kml
args = {}
rake aborted!
can't convert nil into String

所以,文件名没有进入参数。我不知道我在这里做错了什么......

最佳答案

试试这样的片段:

namespace :tale do
desc "Expects to get a file or folder ..."
task(:import_kml, [:filename]) do |t, args|
args.with_default(:filename => :environment)
puts "args = #{args.inspect}"
end
end

rake tale:import_kml[foo] # => args = {:filename => "foo"}

关于ruby - 无法将 args 传递给 rake 任务 - 语法是否已更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8268877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com