On Fri, Aug 28, 2009 at 6:03 AM, Pete Moran<pete / zoborg.com> wrote: > Hi there, > > I am a total newbie to Ruby, so please bare with me. > > I want to be able to be able to initialise a Class with a hash. ¨Βος > instance If I have the class > > class Vehicle > ¨Βττςίαγγεσσος ΊφτωπεΊδεσγςιπτιοξΊφεθιγμεΊγαπαγιτ> end > > I want to be able to do > > Vehicle.new(:vtype => 'ECAR', :description => 'Great Car', :vehicle => > 'Ford Focus', :capacity => '10'); > > Now I could do something like > > def initialize(items) > @vtype = items[:vtype] > @description = items[:description] > @vehicle = items[:vehicle] > @capacity = items[:capacity] > # puts items.inspect > end > > But I was wondering if there was a more generic way of doing this > without specifically defining each variable in initialize? I think > ActiveRecord does something similar! > > Many thanks for your assist! Just playing devil's advocate, what about using OpenStruct? require 'ostruct' class Vehicle < OpenStruct end v1 = Vehicle.new(:vtype => 'ECAR', :description => 'Great Car', :vehicle => 'Ford Focus', :capacity => '10') I guess the concern would be the possibility of adding additional accessors. Using attr_accessor inside the class seems to keep me from updating the variables.