Ruby convert Object to Hash

RubyObjectHashmapInstance Variables

Ruby Problem Overview


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)?

Ruby Solutions


Solution 1 - Ruby

Just say (current object) .attributes

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

Solution 2 - Ruby

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}

Alternatively with each_with_object:

gift = Gift.new
hash = gift.instance_variables.each_with_object({}) { |var, hash| hash[var.to_s.delete("@")] = gift.instance_variable_get(var) }
p hash # => {"name"=>"book", "price"=>15.95}

Solution 3 - Ruby

Implement #to_hash?

class Gift
  def to_hash
    hash = {}
    instance_variables.each { |var| hash[var.to_s.delete('@')] = instance_variable_get(var) }
    hash
  end
end


h = Gift.new("Book", 19).to_hash

Solution 4 - Ruby

Gift.new.instance_values # => {"name"=>"book", "price"=>15.95}

Solution 5 - Ruby

You can use as_json method. It'll convert your object into hash.

But, that hash will come as a value to the name of that object as a key. In your case,

{'gift' => {'name' => 'book', 'price' => 15.95 }}

If you need a hash that's stored in the object use as_json(root: false). I think by default root will be false. For more info refer official ruby guide

http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json

Solution 6 - Ruby

For Active Record Objects

module  ActiveRecordExtension
  def to_hash
    hash = {}; self.attributes.each { |k,v| hash[k] = v }
    return hash
  end
end

class Gift < ActiveRecord::Base
  include ActiveRecordExtension
  ....
end

class Purchase < ActiveRecord::Base
  include ActiveRecordExtension
  ....
end

and then just call

gift.to_hash()
purch.to_hash() 

Solution 7 - Ruby

class Gift
  def to_hash
    instance_variables.map do |var|
      [var[1..-1].to_sym, instance_variable_get(var)]
    end.to_h
  end
end

Solution 8 - Ruby

If you are not in an Rails environment (ie. don't have ActiveRecord available), this may be helpful:

JSON.parse( object.to_json )

Solution 9 - Ruby

You can write a very elegant solution using a functional style.

class Object
  def hashify
    Hash[instance_variables.map { |v| [v.to_s[1..-1].to_sym, instance_variable_get v] }]
  end
end

Solution 10 - Ruby

Recursively convert your objects to hash using 'hashable' gem (https://rubygems.org/gems/hashable) Example

class A
  include Hashable
  attr_accessor :blist
  def initialize
    @blist = [ B.new(1), { 'b' => B.new(2) } ]
  end
end

class B
  include Hashable
  attr_accessor :id
  def initialize(id); @id = id; end
end

a = A.new
a.to_dh # or a.to_deep_hash
# {:blist=>[{:id=>1}, {"b"=>{:id=>2}}]}

Solution 11 - Ruby

You should override the inspect method of your object to return the desired hash, or just implement a similar method without overriding the default object behaviour.

If you want to get fancier, you can iterate over an object's instance variables with object.instance_variables

Solution 12 - Ruby

Might want to try instance_values. That worked for me.

Solution 13 - Ruby

Produces a shallow copy as a hash object of just the model attributes

my_hash_gift = gift.attributes.dup

Check the type of the resulting object

my_hash_gift.class
=> Hash

Solution 14 - Ruby

To plagiarize @Mr. L in a comment above, try @gift.attributes.to_options.

Solution 15 - Ruby

If you need nested objects to be converted as well.

# @fn       to_hash obj {{{
# @brief    Convert object to hash
#
# @return   [Hash] Hash representing converted object
#
def to_hash obj
  Hash[obj.instance_variables.map { |key|
    variable = obj.instance_variable_get key
    [key.to_s[1..-1].to_sym,
      if variable.respond_to? <:some_method> then
        hashify variable
      else
        variable
      end
    ]
  }]
end # }}}

Solution 16 - Ruby

Gift.new.attributes.symbolize_keys

Solution 17 - Ruby

To do this without Rails, a clean way is to store attributes on a constant.

class Gift
  ATTRIBUTES = [:name, :price]
  attr_accessor(*ATTRIBUTES)
end

And then, to convert an instance of Gift to a Hash, you can:

class Gift
  ...
  def to_h
    ATTRIBUTES.each_with_object({}) do |attribute_name, memo|
      memo[attribute_name] = send(attribute_name)
    end
  end
end

This is a good way to do this because it will only include what you define on attr_accessor, and not every instance variable.

class Gift
  ATTRIBUTES = [:name, :price]
  attr_accessor(*ATTRIBUTES)

  def create_random_instance_variable
    @xyz = 123
  end

  def to_h
    ATTRIBUTES.each_with_object({}) do |attribute_name, memo|
      memo[attribute_name] = send(attribute_name)
    end
  end
end

g = Gift.new
g.name = "Foo"
g.price = 5.25
g.to_h
#=> {:name=>"Foo", :price=>5.25}

g.create_random_instance_variable
g.to_h
#=> {:name=>"Foo", :price=>5.25}

Solution 18 - Ruby

I started using structs to make easy to hash conversions. Instead of using a bare struct I create my own class deriving from a hash this allows you to create your own functions and it documents the properties of a class.

require 'ostruct'

BaseGift = Struct.new(:name, :price)
class Gift < BaseGift
  def initialize(name, price)
    super(name, price)
  end
  # ... more user defined methods here.
end

g = Gift.new('pearls', 20)
g.to_h # returns: {:name=>"pearls", :price=>20}

Solution 19 - Ruby

Following Nate's answer which I haven't been able to compile:

Option 1

class Object
    def to_hash
        instance_variables.map{ |v| Hash[v.to_s.delete("@").to_sym, instance_variable_get(v)] }.inject(:merge)
   end
end

And then you call it like that:

my_object.to_hash[:my_variable_name]

Option 2

class Object
    def to_hash
        instance_variables.map{ |v| Hash[v.to_s.delete("@"), instance_variable_get(v)] }.inject(:merge)
   end
end

And then you call it like that:

my_object.to_hash["my_variable_name"]

Solution 20 - Ruby

You can use symbolize_keys and in-case you have nested attributes we can use deep_symbolize_keys:

gift.as_json.symbolize_keys => {name: "book", price: 15.95}
 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionma11hew28View Question on Stackoverflow
Solution 1 - RubyAustin MaruscoView Answer on Stackoverflow
Solution 2 - RubyVasiliy ErmolovichView Answer on Stackoverflow
Solution 3 - RubylevinalexView Answer on Stackoverflow
Solution 4 - RubyErik ReedstromView Answer on Stackoverflow
Solution 5 - Rubyvicke4View Answer on Stackoverflow
Solution 6 - Rubymonika mevenkampView Answer on Stackoverflow
Solution 7 - RubytoklandView Answer on Stackoverflow
Solution 8 - RubyAndreas Rayo KniepView Answer on Stackoverflow
Solution 9 - RubyNate SymerView Answer on Stackoverflow
Solution 10 - RubymustafaturanView Answer on Stackoverflow
Solution 11 - RubyDominicView Answer on Stackoverflow
Solution 12 - RubyHunterView Answer on Stackoverflow
Solution 13 - RubySeanView Answer on Stackoverflow
Solution 14 - Rubyhamx0rView Answer on Stackoverflow
Solution 15 - RubySergio SantiagoView Answer on Stackoverflow
Solution 16 - RubyLalit Kumar MauryaView Answer on Stackoverflow
Solution 17 - RubyVinicius BrasilView Answer on Stackoverflow
Solution 18 - RubyAlexander OhView Answer on Stackoverflow
Solution 19 - RubyReimond HillView Answer on Stackoverflow
Solution 20 - RubyA. KUMARView Answer on Stackoverflow