Ruby convert Object to Hash Ask Question

Ruby convert Object to Hash Ask Question

Let's say I have a Gift object with @name = "book" & @price = 15.95. What's the best way to convert that to the Hash {name: "book", price: 15.95} in Ruby, not Rails (although feel free to give the Rails answer too)?

ベストアンサー1

Just say (current object) .attributes

.attributes returns a hash of any object. And it's much cleaner too.

おすすめ記事