Ruby convert Object to Hash
Ruby convert Object to Hash
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)?
Answer by Dominic for Ruby convert Object to Hash
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
Answer by Vasiliy Ermolovich for Ruby convert Object to Hash
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}
Answer by tokland for Ruby convert Object to Hash
class Gift def to_hash instance_variables.map do |var| [var[1..-1].to_sym, instance_variable_get(var)] end.to_h end end
Answer by levinalex for Ruby convert Object to Hash
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
Answer by mcm for Ruby convert Object to Hash
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()
Answer by Erik Reedstrom for Ruby convert Object to Hash
Gift.new.instance_values # => {"name"=>"book", "price"=>15.95}
Answer by andoke for Ruby convert Object to Hash
You should try Hashie, a wonderful gem : https://github.com/intridea/hashie
Answer by Austin Marusco for Ruby convert Object to Hash
Just say (current object) .attributes
.attributes
returns a hash
of any object
. And it's much cleaner too.
Answer by mustafaturan for Ruby convert Object to Hash
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}}]}
Answer by Sean for Ruby convert Object to Hash
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
Answer by Nate Symer for Ruby convert Object to Hash
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
Answer by Andreas Rayo Kniep for Ruby convert Object to Hash
If you are not in an Rails environment (ie. don't have ActiveRecord available), this may be helpful:
JSON.parse( object.to_json )
Answer by Santiago for Ruby convert Object to Hash
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 # }}}
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment