我偶然发现了我认为是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容器中运行的rails应用程序上的ruby。如果有人可以复制,请在下面评论!