Search code examples
ruby-on-railsrubyruby-on-rails-4ruby-on-rails-5ruby-on-rails-6

Problem with passing argument to rake task


I am coding in Ruby 2.3.1p112 and Rails 4.2.7.1 and encounter this bug(?) when trying to use if-statement inside of one of the rake files.

I call this rake task:

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

from this worker:

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

And when the code hits the binding.pry line I get the following issue: blank bug

Is it a bug indeed or am I lacking some basic knowledge around here? Thanks!


Solution

  • You want

    arg[:argument].blank?
    

    because arg is a hash with :argument key.

    On a side note: the following would be more descriptive definition of a task (note plural args and location since it looks like you're passing location):

    task :bar, [:location] => :environment do |_task, args|
      if args[:location].blank?
        # do stuff
      else
        # do other stuff
      end
    end