aetherknight / recursive-open-struct

OpenStruct subclass that returns nested hash attributes as RecursiveOpenStructs
Other
276 stars 54 forks source link

Keys defined as strings return nil values #28

Closed jromigh closed 9 years ago

jromigh commented 9 years ago

In v0.6.2, if a hash where keys are set as strings (as opposed to symbols) is passed into recursive open struct, all values retrieved are returned as nil.

Here is my code:

require 'recursive-open-struct'

# String type
ros = RecursiveOpenStruct.new( { "foo" => { "bar" => "val" }, "another_key" => "another_value", "hello" => "world" } )
puts ros.inspect
puts "foo = #{ros.foo}"
puts "another_key = #{ros.another_key}"
puts "hello = #{ros.hello}"

# Symbol type
ros = RecursiveOpenStruct.new( { :foo => { :bar => "val" }, :another_key => "another_value", :hello => "world" } )
puts ros.inspect
puts "foo = #{ros.foo}"
puts "another_key = #{ros.another_key}"
puts "hello = #{ros.hello}"

In v0.6.2, this is what I get in console:

C:\testing_recursive_ostruct>bundle exec ruby testing_recursive_ostruct.rb
#<RecursiveOpenStruct foo={"bar"=>"val"}, another_key="another_value", hello="world">
foo =
another_key =
hello =
#<RecursiveOpenStruct foo={:bar=>"val"}, another_key="another_value", hello="world">
foo = #<RecursiveOpenStruct bar="val">
another_key = another_value
hello = world

In v0.6.1, this is what I get in console:

C:\testing_recursive_ostruct>bundle exec ruby testing_recursive_ostruct.rb
#<RecursiveOpenStruct foo={"bar"=>"val"}, another_key="another_value", hello="world">
foo = #<RecursiveOpenStruct bar="val">
another_key = another_value
hello = world
#<RecursiveOpenStruct foo={:bar=>"val"}, another_key="another_value", hello="world">
foo = #<RecursiveOpenStruct bar="val">
another_key = another_value
hello = world
celsworth commented 9 years ago

Can confirm this, also just got bitten by it.

0.6.2:

RecursiveOpenStruct.new('foo' => 1).foo
=> nil

0.6.1:

RecursiveOpenStruct.new('foo' => 1).foo
=> 1
thiagogsr commented 9 years ago

https://github.com/aetherknight/recursive-open-struct/pull/30

gerrywastaken commented 9 years ago

Thanks for the fix @thiagogsr.

thiagogsr commented 9 years ago

You are welcome On Apr 11, 2015 10:11 AM, "Gerry" notifications@github.com wrote:

Thanks for the fix @thiagogsr https://github.com/thiagogsr.

— Reply to this email directly or view it on GitHub https://github.com/aetherknight/recursive-open-struct/issues/28#issuecomment-91843661 .

aetherknight commented 9 years ago

Merged #30 into master, I will cut a patch release soon.

aetherknight commented 9 years ago

Version 0.6.3 should now be on rubygems, and it should contain the fix in #30.