aetherknight / recursive-open-struct

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

Unknown key with a block returns nil #60

Closed kke closed 5 years ago

kke commented 5 years ago

This can cause confusion.

  > RecursiveOpenStruct.new(hello: 'world').each { |k, v| puts k.upcase }
  => nil
  > RecursiveOpenStruct.new(hello: 'world').eahc_pair { |k, v| puts k.upcase }
  => nil
  > RecursiveOpenStruct.new(hello: 'world').each_pair { |k, v| puts k.upcase }
  HELLO
  => #<RecursiveOpenStruct hello="world">
  > RecursiveOpenStruct.new(hello: 'world').transform_keys(&:upcase)
  => nil

Unknown key with a block should probably raise NoMethodError.

kke commented 5 years ago

Apparently this also happens with OpenStruct so maybe it should be reported to ruby issue tracker instead.

Edit: Reported as https://bugs.ruby-lang.org/issues/16086

kke commented 5 years ago

While frustrating (pulling hair because struct.each does nothing until realizing there's no .each), this is actually not a bug in either piece.