Bug #11382
closedKeyword arguments randomly nullified
Description
Here is the example:
class Parent
def initialize(a: nil, b: nil, **properties)
puts("parent: { a: #{a.inspect}, b: #{b.inspect}, properties: #{properties}")
end
end
class Child < Parent
def initialize(args={})
puts("child: #{args}")
super
end
end
require "yaml"
Child.new(a: "A", **YAML.load("---\n:b: B\n:x: X")) # ok
Child.new(a: "A", **YAML.load("---\n:b: B\n:xx: XX")) # bug: ":a" gets nullified
The output is:
child: {:a=>"A", :b=>"B", :x=>"X"}
parent: { a: "A", b: "B", properties: {:x=>"X"}
child: {:a=>"A", :b=>"B", :xx=>"XX"}
parent: { a: "A", b: nil, properties: {:xx=>"XX"}
The only difference between first and second example is that the second one uses :xx as extra argument, while the first one uses :x.
The bug will appear randomly based on the argument name.
Also, this only happens when when the arguments are loaded from somewhere else (e.g. YAML.load).
If i put { b: "B", xx: "XX" }
just anywhere in the code, the bug won't happen, which is extra weird.
It works with ruby 2.1, and 2.0.
Updated by zogash (Milovan Zogovic) over 9 years ago
- Subject changed from Keyword arguments nullified on inheritance to Keyword arguments randomly nullified
Actually, this has nothing to do with inheritance. Here is simplified example:
class Bug
def initialize(a:, b: nil, **others)
puts("a: #{a.inspect}, b: #{b.inspect}, others: #{others}")
end
end
require "yaml"
Bug.new(**YAML.load("---\n:a: A\n:b: B\n:cc: CC")) # :b gets nullified
Bug.new(**YAML.load("---\n:a: A\n:b: B\n:c: C")) # :b not nullified
Updated by zogash (Milovan Zogovic) over 9 years ago
My ruby version: "ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]"
Updated by nagachika (Tomoyuki Chikanaga) over 9 years ago
- Related to Bug #11027: Named Argument assignment from Hash failure added
Updated by nagachika (Tomoyuki Chikanaga) over 9 years ago
- Status changed from Open to Closed
Hello, Thank you for your report.
I think it is related to #11027 and already finxed on trunk and ruby_2_2 branch HEAD.
$ ruby -v bug11382.rb
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
a: "A", b: nil, others: {:cc=>"CC"}
a: "A", b: "B", others: {:c=>"C"}
$ rbenv shell 2.2.0-dev
$ ruby -v bug11382.rb
ruby 2.2.3p150 (2015-08-04 revision 51475) [x86_64-darwin14]
a: "A", b: "B", others: {:cc=>"CC"}
a: "A", b: "B", others: {:c=>"C"}
Could you try with ruby_2_2 HEAD? If you use rbenv and ruby-build, you can install it as 2.2.0-dev.
Updated by zogash (Milovan Zogovic) over 9 years ago
I've tried with 2.2.0-dev and it worked. Thanks!