Comments on: Convert a Ruby hash into a class object http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/ Fri, 25 May 2012 10:47:20 +0000 hourly 1 https://wordpress.org/?v=5.6 By: zx12r http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-18935 Fri, 25 May 2012 10:47:20 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-18935 Found a class inside hashie gem called “mash” which does exactly this

https://github.com/intridea/hashie#mash

]]>
By: Tom Cocca http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-18890 Thu, 03 May 2012 20:26:57 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-18890 I found a much faster way to accomplish the same thing as your initialize method. I was seeing very slow times initializing a set of objects so I re-wrote the #initialize method as follows:

def initialize(hash)
hash.each do |key, value|
self.instance_variable_set(“@#{key}”, value)
self.class.send(:attr_reader, key)
end
end

That creates attr_reader methods to access the instance variables. If you want to be able to read and write to those methods just use an attr_accessor:

def initialize(hash)
hash.each do |key, value|
self.instance_variable_set(“@#{key}”, value)
self.class.send(:attr_accessor, key)
end
end

I found this code to run much faster than the:
self.class.send(:define_method, k, proc{self.instance_variable_get(“@#{k}”)})
self.class.send(:define_method, “#{k}=”, proc{|v| self.instance_variable_set(“@#{k}”, v)})

Thanks,
~ Tom

]]>
By: .jpg http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-18805 Fri, 17 Feb 2012 22:25:31 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-18805 Can this be accomplished with ActiveModel? It looks like you’re already in rails.

Thanks so much for these ideas! I took them and modified them a little to make them slightly more extendible/useable.

https://gist.github.com/1855631

Thoughts? Please comment on the gist…

.jpg

]]>
By: Simone http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-18620 Thu, 27 Oct 2011 14:49:05 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-18620 Hello,
this is how I changed initialize() method to make it recurse on value that are hash themselves:

class HashIt
def initialize(hash)
hash.each do |k,v|
case v
when Hash; self.instance_variable_set(“@#{k}”, HashIt.new(v))
else; self.instance_variable_set(“@#{k}”, v)
end
self.class.send(:define_method, k, proc{ self.instance_variable_get(“@#{k}”) })
self.class.send(:define_method, “#{k}=”, proc{ |v| self.instance_variable_set(“@#{k}”, v)})
end
end
end

thank you for your post 🙂

]]>
By: Sheldon Hearn http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-18578 Tue, 27 Sep 2011 05:52:22 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-18578 I’m keen to steal your idea of combining option splatting with accessor definition, into magic_options:

http://github.com/sheldonh/magic_options

So instead of

class Cow
include MagicOptions
magic_initialize :only => respond_to?
attr_accessor :name, :color, :gender
end

we could support

class Cow
include MagicOptions
magic_initialize :accessors => [:name, :color, :gender]
end

]]>
By: Avram http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-18263 Fri, 17 Dec 2010 19:57:16 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-18263 Somehow that code snippet got frotzed. Here it is again, without the prompts, and with < instead of a less-than sign for for class inheritance, in opes that the server doesn’t digest it:

class Foo < ActiveResource::Base
self.site = ”
end
f = Foo.new(:a=>1,:b=>2,:c=>{:d=>3,:e=>4})
(returns ruby object)
f.c.d
(returns 3)

]]>
By: Avram http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-18262 Fri, 17 Dec 2010 19:53:54 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-18262 Hello,

I didn’t read this in too much detail because I found another solution. Perhaps this misses the point, if so I apologize.

ActiveResource can actually do this for you. This is essentially what ARes already does when it gets a hash back from a ReST interface. You just need a dummy ARes-based model (or you can use a legit one if you have it). All you do is call #new on it, and pas the hash in. self.site is a required setting for ARes classes, but it can be empty.

>> class Foo > Foo.new(:a=>1,:b=>2,:c=>{:d=>3,:e=>4})
=> #1, “b”=>2, “c”=>#3, “e”=>4}>}>
>> f.c.d
=> 3
>>

]]>
By: zx12r http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-15604 Fri, 22 Oct 2010 11:13:45 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-15604 made a small change in the save method for address nested ones too

def save(object = self)
hash_to_return = {}
object.instance_variables.each do |var|
instance_variable_value = object.instance_variable_get(var)
if instance_variable_value.is_a?(HashToObject)
hash_value = get_hash(instance_variable_value)
hash_to_return[var.gsub(“@”,””)] = hash_value
else
hash_to_return[var.gsub(“@”,””)] = instance_variable_value
end
end
return hash_to_return
end

]]>
By: taobao http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-13499 Wed, 08 Sep 2010 16:37:56 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-13499 class Hashit < OpenStruct
module DummyMethodMissing
def method_missing(*args, &block)
raise NoMethodError
end
end

]]>
By: Emmanuel http://pullmonkey.com/2008/01/06/convert-a-ruby-hash-into-a-class-object/comment-page-1/#comment-11566 Wed, 21 Jul 2010 02:26:34 +0000 /2008/08/06/convert-a-ruby-hash-into-a-class-object#comment-11566 how about:

require ‘ostruct’
class Hashit < OpenStruct
def save
self.class.new(@table)
end
end

Although it has the annoying property of returning nil to all methods that are not defined. You could “fix” that like this:

require ‘ostruct’

class Hashit < OpenStruct
module DummyMethodMissing
def method_missing(*args, &block)
raise NoMethodError
end
end

def initialize(*args)
super(*args)
extend DummyMethodMissing
end
end

]]>