rubywhenever

Ruby curly bracket block syntax is not working while do...end works


I am using this gem. When I use the following syntax, it works fine:

every :day do
  rake 'billing:daily'
end

However, when I use the following syntax, the gem is giving me syntax error:

every :day { rake 'billing:daily' }

Output:

~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/whenever-0.10.0/lib/whenever/job_list.rb:25:in `instance_eval': config/schedule.rb:26: syntax error, unexpected '{', expecting end-of-input (SyntaxError)
every :day { rake 'billing:daily' }
            ^
    from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/whenever-0.10.0/lib/whenever/job_list.rb:25:in `initialize'
    from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/whenever-0.10.0/lib/whenever.rb:12:in `new'
    from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/whenever-0.10.0/lib/whenever.rb:12:in `cron'
    from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/whenever-0.10.0/lib/whenever/command_line.rb:42:in `run'
    from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/whenever-0.10.0/lib/whenever/command_line.rb:6:in `execute'
    from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/whenever-0.10.0/bin/whenever:44:in `<top (required)>'
    from ~/.rbenv/versions/2.4.3/bin/whenever:23:in `load'
    from ~/.rbenv/versions/2.4.3/bin/whenever:23:in `<main>'

Aren't both the same thing? Why is the former working but not the latter?


Solution

  • It's a parsing/precedence issue. Braces try to bind to the nearest token, which is :day in this case, but you want it to bind to every(). You have to write every(:day) { rake 'billing:daily' } to explicitly bind it to the correct token.