Creating an Expando object in Ruby

Posted by tyndall on Stack Overflow See other posts from Stack Overflow or by tyndall
Published on 2011-01-16T03:31:29Z Indexed on 2011/01/16 3:53 UTC
Read the original article Hit count: 193

Filed under:
|

Is there a better way to write this Expando class? The way it is written does not work. I'm using Ruby 1.8.7

starting code quoted from https://gist.github.com/300462/3fdf51800768f2c7089a53726384350c890bc7c3

class Expando
    def method_missing(method_id, *arguments)
        if match = method_id.id2name.match(/(\w*)(\s*)(=)(\s*)(\.*)/)
              puts match[1].to_sym # think this was supposed to be commented 
              self.class.class_eval{ attr_accessor match[1].to_sym } 
              instance_variable_set("#{match[1]}", match[5])
        else
              super.method_missing(method_id, *arguments)
        end  
    end    
end

person = Expando.new 
person.name = "Michael"
person.surname = "Erasmus"
person.age = 29 

© Stack Overflow or respective owner

Related posts about ruby

Related posts about expando