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

Version Path
attributes1-5.0.3 samples/d.rb
attributes1-5.0.2 samples/d.rb
attributes-3.2.0 samples/d.rb
attributes-3.0.0 samples/d.rb
attributes-4.1.0 samples/d.rb
attributes-5.0.0 samples/d.rb
attributes-3.5.0 samples/d.rb
attributes-3.0.1 samples/d.rb
attributes-3.3.0 samples/d.rb
attributes-3.6.0 samples/d.rb
attributes-3.7.0 samples/d.rb
attributes-5.0.1 samples/d.rb
attributes-4.0.0 samples/d.rb