更新到 Ruby 3.0 后出现 ArgumentError(参数数量错误(给定 2,预期 1))
ArgumentError (wrong number of arguments (given 2, expected 1)) after updating to Ruby 3.0
当我尝试将我们的站点更新到 Ruby 3.0.0 时,出现此错误:
ArgumentError(参数数量错误(给定 2,预期 1))
% rails console
Loading development environment (Rails 6.1.0)
irb(main):001:0> puts RUBY_VERSION
3.0.0
irb(main):002:0> puts IceCube::VERSION
0.16.3
irb(main):003:0> schedule = IceCube::Schedule.new
=> #<IceCube::Schedule:0x00007fccfe19cfa8 @start_time=2020-12-27 11:14:30 -0800, @all_recurrence_rules=[], @all_exception_rules=[]>
irb(main):004:0> puts schedule.to_ical
Traceback (most recent call last):
1: from (irb):4:in `<main>'
ArgumentError (wrong number of arguments (given 2, expected 1))
这里是与 Ruby 2.7.2 相同的命令
% rails console
Loading development environment (Rails 6.1.0)
irb(main):001:0> puts RUBY_VERSION
2.7.2
irb(main):002:0> puts IceCube::VERSION
0.16.3
irb(main):003:0> schedule = IceCube::Schedule.new
=> #<IceCube::Schedule:0x00007f9da3128fe8 @start_time=2020-12-27 11:12:50 -0800, @all_recurrence_rules=[], @all_exception_rules=[]>
irb(main):004:0> puts schedule.to_ical
DTSTART;TZID=PST:20201227T111250
令人困惑的是,它也适用于普通 ruby 脚本,与第一个示例相同的 gem 版本
% irb
irb(main):001:0> puts RUBY_VERSION
3.0.0
irb(main):002:0> gem 'ice_cube'
irb(main):003:0> require 'ice_cube'
irb(main):004:0> puts IceCube::VERSION
0.16.3
irb(main):005:0> schedule = IceCube::Schedule.new
=> #<IceCube::Schedule:0x00007ff2fb0d5b88 @start_time=2020-12-27 11:11:02 -0800, @all_recurrence_rules=[], @all_exception_rules=[]>
irb(main):006:0> puts schedule.to_ical
DTSTART;TZID=PST:20201227T111102
有谁知道我可以从哪里着手解决这个问题?我们刚刚开始这个项目,我希望使用 Ruby 3
问题与 how keyword arguments are handled in ruby 3.0 and how ice_cube
is passing its arguments to I18n.localize
有关。
因此,提取并简化错误代码,这对 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:{}
从 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))
因此,我看到的修复方法是将参数显式传递给方法:
def foo2(object, **options)
bar(object, **options)
end
foo2('date', format: 'whatever')
# object:date
# locale:
# format:whatever
# options:{}
module IceCube
module I18n
def self.l(object, **options)
backend.l(object, **options)
end
end
end
并发送 PR,使用你自己的 fork,monkey-patch,任何最适合你的。
但最重要的是,准备好在很多其他的 gem 中发现这个问题,因为第一个稳定的 ruby 3 版本刚刚发布 2 days ago,所以你可能会在其他 gem 中遇到这些类型的问题,特别是未维护的。
祝你好运。
更新
看你的 PR 我不记得你修改的部分代码,然后我意识到我专注于 0.16.3
版本,也就是你在问题中提到的版本。事情是,master
分支比上一个 0.16.3
版本有更多的变化,特别是 this change 将 locale/translate 方法委托给后端,在天,比我的解决方案更好,它应该可以解决您的问题。
所以,在你的 Gemfile
gem 'ice_cube', git: 'https://github.com/seejohnrun/ice_cube.git', branch: :master
应该可以解决问题。无论如何,我会留下我之前的回复作为一般性问题的任何人的参考,而不是专门针对 ice_cube
.
当我尝试将我们的站点更新到 Ruby 3.0.0 时,出现此错误:
ArgumentError(参数数量错误(给定 2,预期 1))
% rails console
Loading development environment (Rails 6.1.0)
irb(main):001:0> puts RUBY_VERSION
3.0.0
irb(main):002:0> puts IceCube::VERSION
0.16.3
irb(main):003:0> schedule = IceCube::Schedule.new
=> #<IceCube::Schedule:0x00007fccfe19cfa8 @start_time=2020-12-27 11:14:30 -0800, @all_recurrence_rules=[], @all_exception_rules=[]>
irb(main):004:0> puts schedule.to_ical
Traceback (most recent call last):
1: from (irb):4:in `<main>'
ArgumentError (wrong number of arguments (given 2, expected 1))
这里是与 Ruby 2.7.2 相同的命令
% rails console
Loading development environment (Rails 6.1.0)
irb(main):001:0> puts RUBY_VERSION
2.7.2
irb(main):002:0> puts IceCube::VERSION
0.16.3
irb(main):003:0> schedule = IceCube::Schedule.new
=> #<IceCube::Schedule:0x00007f9da3128fe8 @start_time=2020-12-27 11:12:50 -0800, @all_recurrence_rules=[], @all_exception_rules=[]>
irb(main):004:0> puts schedule.to_ical
DTSTART;TZID=PST:20201227T111250
令人困惑的是,它也适用于普通 ruby 脚本,与第一个示例相同的 gem 版本
% irb
irb(main):001:0> puts RUBY_VERSION
3.0.0
irb(main):002:0> gem 'ice_cube'
irb(main):003:0> require 'ice_cube'
irb(main):004:0> puts IceCube::VERSION
0.16.3
irb(main):005:0> schedule = IceCube::Schedule.new
=> #<IceCube::Schedule:0x00007ff2fb0d5b88 @start_time=2020-12-27 11:11:02 -0800, @all_recurrence_rules=[], @all_exception_rules=[]>
irb(main):006:0> puts schedule.to_ical
DTSTART;TZID=PST:20201227T111102
有谁知道我可以从哪里着手解决这个问题?我们刚刚开始这个项目,我希望使用 Ruby 3
问题与 how keyword arguments are handled in ruby 3.0 and how ice_cube
is passing its arguments to I18n.localize
有关。
因此,提取并简化错误代码,这对 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:{}
从 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))
因此,我看到的修复方法是将参数显式传递给方法:
def foo2(object, **options)
bar(object, **options)
end
foo2('date', format: 'whatever')
# object:date
# locale:
# format:whatever
# options:{}
module IceCube
module I18n
def self.l(object, **options)
backend.l(object, **options)
end
end
end
并发送 PR,使用你自己的 fork,monkey-patch,任何最适合你的。
但最重要的是,准备好在很多其他的 gem 中发现这个问题,因为第一个稳定的 ruby 3 版本刚刚发布 2 days ago,所以你可能会在其他 gem 中遇到这些类型的问题,特别是未维护的。
祝你好运。
更新
看你的 PR 我不记得你修改的部分代码,然后我意识到我专注于 0.16.3
版本,也就是你在问题中提到的版本。事情是,master
分支比上一个 0.16.3
版本有更多的变化,特别是 this change 将 locale/translate 方法委托给后端,在天,比我的解决方案更好,它应该可以解决您的问题。
所以,在你的 Gemfile
gem 'ice_cube', git: 'https://github.com/seejohnrun/ice_cube.git', branch: :master
应该可以解决问题。无论如何,我会留下我之前的回复作为一般性问题的任何人的参考,而不是专门针对 ice_cube
.