Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct being converted #249

Open
mauricioszabo opened this issue Jul 22, 2019 · 3 comments
Open

Struct being converted #249

mauricioszabo opened this issue Jul 22, 2019 · 3 comments

Comments

@mauricioszabo
Copy link

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.

@dubek
Copy link
Contributor

dubek commented Jul 24, 2019

I looked at the history and it seems that this behaviour was deliberately added in commit e7a8f12 (includes tests-cases). However, as you mention, the documentation doesn't mention this one-way behaviour: Hamster.from converts Struct to Hamster::Hash but Hamster.to_ruby doesn't (cannot) convert it back.

A PR with documentation changes for Hamster.from and Hamster.to_ruby would be appreciated...

@alexdowad
Copy link
Contributor

alexdowad commented Jul 26, 2019 via email

@mauricioszabo
Copy link
Author

I saw that it was deliberate too, but I think that it probably violates the principle of less surprise, as it looses information on the conversion (something that don't happen if I try to convert only hashes, arrays and sets).

I would vote for another solution on Struct's conversion, but if it's not possible an update on documentation would be great too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants