Search code examples
ruby-on-railshashrake

Pass Hash as Parameter to Rake Task


I've got a rake task

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

:p needs to 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},{}]

It errors out saying that id: could not be parsed (the space seemed to do 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"},{}]

the who string "id: 5" was being interpreted as a single string.

Are we not allowed to pass hashes to rake tasks?


Solution

  • When you issue a rake task via the terminal, you are in the UNIX or Bash environment, so you need to obey the rules of that language, which as far as I can tell doesn't include hashes. I could be wrong though.

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

    This doesn't mean that whoever wrote the rake task didn't do something clever to parse a string into a hash. If I were you and trying to figure it out I would have a look at the source code and see how that rake task parses it's variables.

    PS. If it's a gem you are using and you are using bundler as well, you can issue bundle open gem_name to open up the source, rake tasks generally end in a .rake...