New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveJob should support passing of keyword arguments to perform method #18741

Closed
threadhead opened this Issue Jan 30, 2015 · 5 comments

Comments

Projects
None yet
4 participants
@threadhead

threadhead commented Jan 30, 2015

Since Rails 5+ will required Ruby 2.2+, ActiveJob should support the passing of keyword arguments to the perform method.

class MyAwesomeJob < ActiveJob::Base
  def perform(ginger_ninjas: 1)
    attack!(ginger_ninjas)
  end
end

MyAwesomeJob.perform_later(ginger_ninjas: 5)

Currently (rails 4.2), you get a ArgumentError: wrong number of arguments (1 for 0) if you try to pass keyword arguments.

@sgrif sgrif added the activejob label Jan 30, 2015

@sgrif sgrif added this to the 4.2.1 milestone Jan 30, 2015

@sgrif

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Jan 30, 2015

Member

This appears to be due to the hash getting converted to string keys during job serialization/deserialization, while keyword arguments only work with symbol keyed hashes.

Member

sgrif commented Jan 30, 2015

This appears to be due to the hash getting converted to string keys during job serialization/deserialization, while keyword arguments only work with symbol keyed hashes.

sgrif added a commit that referenced this issue Jan 30, 2015

Allow keyword arguments to work with ActiveJob
Unfortunately, the HashWithIndifferent access approach is insufficient
for our needs. It's perfectly reasonable to want to use keyword
arguments with Active Job, which we will see as a symbol keyed hash. For
Ruby to convert this back to keyword arguments, it must deserialize to a
symbol keyed hash.

There are two primary changes to the serialization behavior. We first
treat a HWIA separately, and mark it as such so we can convert it back
into a HWIA during deserialization.

For normal hashes, we keep a list of all symbol keys, and convert them
back to symbol keys after deserialization.

Fixes #18741.

Conflicts:
	activejob/CHANGELOG.md

@sgrif sgrif closed this in 31085a5 Jan 30, 2015

@sgrif

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Jan 30, 2015

Member

Backported in 31085a5

Member

sgrif commented Jan 30, 2015

Backported in 31085a5

@threadhead

This comment has been minimized.

Show comment
Hide comment
@threadhead

threadhead Jan 30, 2015

@sgrif Impressive. I was going to start hacking on it after I got back from lunch, but you beat me to it. Thank you.

threadhead commented Jan 30, 2015

@sgrif Impressive. I was going to start hacking on it after I got back from lunch, but you beat me to it. Thank you.

tgxworld added a commit to ruby-bench/ruby-bench-web that referenced this issue Feb 13, 2015

@chrisnicola

This comment has been minimized.

Show comment
Hide comment
@chrisnicola

chrisnicola Feb 18, 2016

Contributor

This is awesome. I was testing it out with Ruby 4.2.4 and noticed that if you mix keyword and regular arguments it doesn't work. For example:

perform(message, channel: '#default')

Won't work and gives you a ArgumentError: wrong number of arguments (2 for 1) error.

perform(message:, channel: '#default')

Does.

Contributor

chrisnicola commented Feb 18, 2016

This is awesome. I was testing it out with Ruby 4.2.4 and noticed that if you mix keyword and regular arguments it doesn't work. For example:

perform(message, channel: '#default')

Won't work and gives you a ArgumentError: wrong number of arguments (2 for 1) error.

perform(message:, channel: '#default')

Does.

@liaogz82

This comment has been minimized.

Show comment
Hide comment
@liaogz82

liaogz82 May 4, 2018

curious, can I use it for perform_later??

liaogz82 commented May 4, 2018

curious, can I use it for perform_later??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment