4

当我尝试将我们的网站更新到 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

令人困惑的是,它也适用于与第一个示例具有相同 gem 版本的普通 ruby​​ 脚本

% 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

4

1 回答 1

4

这个问题与ruby​​ 3.0 中如何处理关键字参数以及如何ice_cube其参数传递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:{}

从红宝石> = 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 天前发布,所以您可能会在其他 gem 中遇到这些类型的问题,特别是未维护的那些。祝你好运。


更新

查看您的 PR,我不记得您修改它的部分代码,然后我意识到我专注于0.16.3版本,这是您在问题中提到的版本。事情是,master分支比上一个版本有更多的变化0.16.3,特别是这个变化将语言环境/翻译方法委托给后端,在一天结束时,这是一个比我更好的解决方案,它应该解决你的问题.
所以,在你的Gemfile

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

应该做的伎俩。无论如何,我会将我之前的回复作为一般性问题的参考,而不是专门针对ice_cube.

于 2020-12-27T23:32:35.423 回答