I love keyword arguments when they were introduced in Ruby 2.0, and use them every chance I get. As I was hacking around with ActiveJob today, and thought it would be cool to pass keyword arguments into the ActiveJob#perform_later method.
I thought it should work. It didn’t.
1 2 3 4 5 6 7 |
class MyAwesomeJob < ActiveJob::Base def perform(ginger_ninjas: 1) attack!(ginger_ninjas) end end MyAwesomeJob.perform_later(ginger_ninjas: 5) |
You get an ArgumentError: wrong number of arguments (1 for 0) error. The reason is the hash is converted to string keys upon serialization.
So I was thrilled when I did a bit of searching to see if anyone else had the same issue. Nope. Looked at the ActiveJob::Arguments module. Passing keyword arguments are not supported. I actually get to report a real feature request to Rails!
So here is my ActiveJob should support passing of keyword arguments to perform method.
This this is where the magic began… within a few minutes Sean Griffin (@sgrif) picked up on the issue report. I went to lunch, planning to fork ActiveJob and start hacking away when I got back. But Sean beat me to it. Sean added the ability to pass keyword arguments to ActiveJob jobs, and even backported it to Rails 4.2.1.
Within an hour, it was all done. Check out the full commit.
Want a reason to love Rails and their committers? This is one, of many.
Thank you Sean.
Hey Karl,
I bumped into the same issue you reported a while ago (thanks for reporting it!) and after reading the comment thread… i couldn’t believe the turn around time from sgrif. Sean Griffin is just an all around badass!