Pass hash as parameter for task markup

I have a rake task

task :post_hit, [:host, :title, :description, :model, :num_assignments, :reward, :lifetime, :qualifications, :p, :opts] => :environment do |t, args| 

:p should be a hash, but if I try:

 rake turkee:post_hit["http://www.staging.collegesportsbluebook.com","LS Info","Fill In Player First Name","MechanicalTurk",100,0.01,2,{},{id: 5},{}] 

Mistakenly says that id: cannot be parsed (space seemed to be doing something).

If I tried:

 rake turkee:post_hit["http://www.staging.collegesportsbluebook.com","LS Info","Fill In Player First Name","MechanicalTurk",100,0.01,2,{},{"id: 5"},{}] 

which line "id: 5" interpreted as one line.

Are we not allowed to pass hashes to perform rake tasks?

+6
source share
3 answers

When you run the rake task through the terminal, you are in a UNIX or Bash environment, so you need to obey the rules of this language, which, as far as I can tell, do not include hashes. Maybe I'm wrong.

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html#toc5

This does not mean that the person who wrote the rake task did not do something clever to parse the string into a hash. If I were you and tried to figure it out, I would look at the source code and see how this rake task analyzes its variables.

PS. If this is the gem that you use and you also use satellite, you can release bundle open gem_name to open the source, rake tasks usually end in .rake ...

+1
source

I use parameters similar to the query and parse them using Rack::Utils.parse_nested_query

This is how i do it

 require 'rack/utils' # needed for Rack::Utils.parse_nested_query namespace :foo do task :bar, [ :args_expr ] => :environment do |t, args| args.with_defaults(:args_expr => "name=abdo&fruits[]=bananas&fruits[]=dates") options = Rack::Utils.parse_nested_query(args[:args_expr]) puts options end end 

And I call it this way: (notice how arrays and hashes are passed)

 bundle exec rake "foo:bar[name=abdo&fruits[]=apples&fruits[]=oranges&hash[foo]=bar&hash[cool]=notmuch]" 

Conclusion:

 {"name"=>"abdo", "fruits"=>["apples", "oranges"], "hash"=>{"foo"=>"bar", "cool"=>"notmuch"}} 
+9
source

Try declaring your p variable: as a hash in the task ?: p => {}

Task

: post_hit, [: host ,: title ,: description ,: model ,: num_assignments ,: reward ,: lifetime ,: qualifications ,: p => {} ,: opts

0
source

Source: https://habr.com/ru/post/915327/


All Articles