Search code examples
rubysyntax-errortaskbundlerake

Ruby rake prints argument but doesn't like it's value on methodcalll


I have task:

desc "Create a team"
task :create_dev_team, [:team_name] do |t, args|
  puts "Creating \"#{args.team_name}\" team under Sub Org ID: 1"
  ESP::Team.create(name: #{args.team_name}, sub_organization_id: 1)
end

The ESP SDK Documentation to create an team is :

team = ESP::Team.create(name: "Team Name", sub_organization_id: 6)

when I call that line directly from irb it works fine,

BUT when I call :

rake create_dev_team[Team Name] --trace

I get:

rake aborted!

SyntaxError: /Users/me/dev/my-repo/Rakefile:348: syntax error, unexpected keyword_end

What am I doing wrong?


Solution

  • You are using syntax as if you were interpolating a string:

    #{args.team_name}
    

    But without quoting the string - there is no assumed string here. That means Ruby will treat it as a comment starting at the #. Which in turn means that there is no closing ) in the code. When the parser find the end, it is unexpected, since it still thinks the context is inside the method call, so it cannot finish the outer block.

    You should either use:

    ESP::Team.create(name: args.team_name, sub_organization_id: 1)
    

    Or:

    ESP::Team.create(name: "#{args.team_name}", sub_organization_id: 1)
    

    The former is what you should use if your method always returns a String. Here, args.team_name could return nil. But you should really check for that earlier and fail the task if so, as an empty team name may cause you problems elsewhere in your code.