Sha256: 28d7a9a49ee7ba1f25ce835101eafdd42444ce6246e15a0b49ce03a143de0ecb
Contents?: true
Size: 665 Bytes
Versions: 13
Compression:
Stored size: 665 Bytes
Contents
# # a nice feature is that all attributes are enumerated in the class. this, # combined with the fact that the getter method is defined so as to delegate # to the setter when an argument is given, means bulk initialization and/or # attribute traversal is very easy. # require 'attributes' class C attributes %w( x y z ) def attributes self.class.attributes end def initialize attributes.each_with_index{|a,i| send a, i} end def to_hash attributes.inject({}){|h,a| h.update a => send(a)} end def inspect to_hash.inspect end end c = C.new p c.attributes p c c.x 'forty-two' p c.x
Version data entries
13 entries across 13 versions & 2 rubygems