class Gift def initialize @name = "book" @price = 15.95 end end gift = Gift.new hash = {} gift.instance_variables.each {|var| hash[var.to_s.delete("@")] = gift.instance_variable_get(var) } p hash # => {"name"=>"book", "price"=>15.95}Source: Stackoverflow
Ruby – Convert Object to Hash
What's the best way to convert an Object to Hash in Ruby?