我偶然发现了我认为是 Ruby 中一个奇怪的错误(我使用的是 2.2.2)。
当我定义这样的方法时:
def test(value, **kw)
puts value
puts kw
end
我得到了预期的输出:
> test(1)
1
{}
=> nil
> test(1, channel: 'active')
1
{:channel=>"active"}
=> nil
> test(1, channel_s: 'active')
1
{:channel_s=>"active"}
=> nil
> test(1, channel_st: 'active')
1
{:channel_st=>"active"}
=> nil
> test(1, channel_sta: 'active')
1
{:channel_sta=>"active"}
=> nil
> test(1, channel_stat: 'active')
1
{:channel_stat=>"active"}
=> nil
> test(1, channel_state: 'active')
1
{:channel_state=>"active"}
=> nil
但是当我用define_singleton_method定义一个等效方法时:
define_singleton_method(:test_2) do |value, **kw|
puts value
puts kw
end
当关键字恰好是“channel_type”时,我会得到一个非常奇特的结果:
> test_2(1)
1
{}
=> nil
> test_2(1, channel_state: 'active')
{:channel_state=>"active"}
=> nil
> test_2(1, channel_stat: 'active')
1
{:channel_stat=>"active"}
=> nil
> test_2(1, channel_states: 'active')
1
{:channel_states=>"active"}
=> nil
> test_2(1, channel_state: nil)
{:channel_state=>nil}
=> nil
有没有人见过这个?也许使用不同的关键字?
编辑:这似乎只会影响我在 docker 容器中运行的 ruby on rails 应用程序。如果有人可以转载,请在下方评论!