Argumenterror (Wrong Number of Arguments (Given 2, Expected 1)) After Updating to Ruby 3.0

ArgumentError (wrong number of arguments (given 2, expected 1)) after updating to Ruby 3.0

The issue has to do with how keyword arguments are handled in ruby 3.0 and how ice_cube is passing its arguments to I18n.localize.

So, extracting and simplifying the buggy code, this worked on ruby < 3

RUBY_VERSION # "2.7.2"

def foo(*args)
bar(*args)
end

def bar(object, locale: nil, format: nil, **options)
puts "object:#{object}"
puts "locale:#{locale}"
puts "format:#{format}"
puts "options:#{options}"
end

foo('date', format: 'whatever')
# object:date
# locale:
# format:whatever
# options:{}

And from ruby >= 3

RUBY_VERSION # "3.0.0"

def foo(*args)
bar(*args)
end

def bar(object, locale: nil, format: nil, **options)
puts "object:#{object}"
puts "locale:#{locale}"
puts "format:#{format}"
puts "options:#{options}"
end

foo('date', format: 'whatever')
# Traceback (most recent call last):
# 16: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/cli.rb:24:in `start'
# 15: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
# 14: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/cli.rb:30:in `dispatch'
# 13: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
# 12: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
# 11: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
# 10: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/cli.rb:497:in `exec'
# 9: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/cli/exec.rb:28:in `run'
# 8: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/cli/exec.rb:63:in `kernel_load'
# 7: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/3.0.0/bundler/cli/exec.rb:63:in `load'
# 6: from /Users/alter/.rbenv/versions/3.0.0/bin/irb:23:in `<top (required)>'
# 5: from /Users/alter/.rbenv/versions/3.0.0/bin/irb:23:in `load'
# 4: from /Users/alter/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/irb-1.3.0/exe/irb:11:in `<top (required)>'
# 3: from (irb):11:in `<main>'
# 2: from (irb):3:in `foo'
# 1: from (irb):5:in `bar'
# ArgumentError (wrong number of arguments (given 2, expected 1))

So, the fix I can see is to explicitely passing the parameters to the method:

def foo2(object, **options)
bar(object, **options)
end

foo2('date', format: 'whatever')
# object:date
# locale:
# format:whatever
# options:{}

Or in terms of the involved module:

module IceCube
module I18n
def self.l(object, **options)
backend.l(object, **options)
end
end
end

and send a PR, use your own fork, monkey-patch, whatever works best for you.

But most important, brace yourself to find this issue in a lot of other gems, as the first stable ruby 3 version was just released 2 days ago, so probably you'll have these type of issues with other gems, specially the unmaintained ones.
Good luck.



Update

Looking your PR I didn't remember part of the code that you modified it, then I realized I was focused on 0.16.3 version, which it was the one you mentioned in the question. Things are, master branch has a lot more changes than the last 0.16.3 version, specially this change where is delegating the locale/translate methods to the backend that, at the end of the day, is a better solution than mine, and it should fix your issue.

So, in your Gemfile

gem 'ice_cube', git: 'https://github.com/seejohnrun/ice_cube.git', branch: :master  

should do the trick. Anyway I'll left my previous response as reference for anyone having the issue in general terms and not specifically with ice_cube.

ArgumentError after updating from Ruby 2.7 to Ruby 3.0.1

As you have shared the link regarding the changes in Ruby 3, the answer is in the link itself:

Separation of positional and keyword arguments in Ruby 3.0:

In most cases, you can avoid incompatibility by adding the double splat operator. It explicitly specifies passing keyword arguments instead of a Hash object. Likewise, you may add braces {} to explicitly pass a Hash object, instead of keyword arguments.

Now looking at your code you are doing this:

job_args = { shop_domain: shop_domain, webhook: webhook_params.to_h }
webhook_job_klass.perform_later(job_args)

i.e., passing a hash to the method instead you intend to pass keyword arguments which you can fix by adding a double splat operator:

webhook_job_klass.perform_later(**job_args)

More info on the error:

The error is:

ArgumentError (wrong number of arguments (given 1, expected 0; required keywords: shop_domain, webhook)):

which means shop_domain and webhook are required keyword arguments and you are passing a single argument as Ruby now treats the hash as a single argument instead of the keyword arguments until you add the double splat operator.

Ruby CSV Open - Wrong number of arguments in Ruby 3.0

In Ruby 3.0, positional arguments and keyword arguments was separated, with deprecation in Ruby 2.7. That means, that the Ruby 2.7 deprecation warning: Using the last argument as keyword parameters is deprecated, ie. using a hash as argument for **options is no longer supported.

You'll have to call it either as:

# Manual spreading hash
CSV.open(csv_path, "wb", **{:col_sep => ";"}) do |csv|
...
end

# Or as Keyword argument

CSV.open(csv_path, "wb", :col_sep => ";") do |csv|
...
end

EDIT: See also https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/

Error persisting records in rails after upgrading to ruby 3.0.1

So... It seems this line in ActiveSupport v6.0.3.6 is calling this method in redis with 3 arguments instead of 2; exactly like the error says!

And just as I suspected, that's already been fixed in the master branch. Here was the commit that introduced the fix.

So in other words, I reckon you've found a bug in rails 6.0 working with ruby 3.0.

Additionally, it seems that this bug has already been backported into the 6.0-stable branch and, according to the comments, "will be included in Rails 6.0.4".


tl;dr: Either downgrade ruby back to 2.7, or upgrade rails to 6.1, or add to your Gemfile:

gem "rails", github: "rails/rails", branch: "6-0-stable"


Related Topics



Leave a reply



Submit