From: Davi Barbosa Date: 2008-11-07T05:44:07+09:00 Subject: Creating object from hash I'm creating an object from a hash using this: class Computer def initialize(hash) hash.each do |k,v| instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair instance_eval %Q{ class << self ; attr_reader #{k.intern.inspect} ; end } end end end This works fine except if I need to define a method with the same name of a key (if I need an complicated getter/setter for example). So I added a test to attr_reader line: instance_eval %Q{ class << self ; attr_reader #{k.intern.inspect} ; end } if !instance_eval %Q{defined? #{k}} As an Object, some methods are defined (like "id"). So "pc = Computer.new({'id' > 1})" does not create the reader for @id and "pc.id" calls some unwanted method. If I just add "attr_reader :id" everything works. Can I do it directly, without adding attr_reader (or attr_accessor) every time that I get an error? -- Posted via http://www.ruby-forum.com/.