Skip to content

Struct being converted #249

Open
Open
@mauricioszabo

Description

@mauricioszabo

On Hamster's Hamster.from documentation, there's no mention that Struct will be converted to a hash, and indeed it is really strange:

class SomeClass < Struct.new(:foo, :bar); end
obj = SomeClass.new(10, 20)
irb(main):014:0> Hamster.from({obj: obj})
# => Hamster::Hash[:obj => Hamster::Hash[:bar => 20, :foo => 10]]

The problem is that SomeClass is not a hash, it is a struct, so this will fail:

a = {obj: SomeClass.new(10, 20)}
b = {obj: SomeClass.new(10, 20)}
Hamster.to_ruby(Hamster.from(a)) == b
# => false

# While these are true:
a == b
Hamster.to_ruby(Hamster.from(a)) == Hamster.to_ruby(Hamster.from(b))

The problem is that Hamster.from loses the struct information, so it's impossible to get back the original object, and the documentation doesn't mention anything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions