Sha256: 9e66ed35c27fad7c0b6bea634e1e1456854e9904f0b7e1f5eef4e1e9ffe0f3d5

Contents?: true

Size: 1.3 KB

Versions: 10

Compression:

Stored size: 1.3 KB

Contents

require 'arrayfields'
#
# the class Array has only a few added method, one is for setting the fields,
# when the fields are set for an array THIS INSTANCE ONLY will be modified to
# allow keyword access.  other arrays will not be affected!
#
  a = [0,1,2]
  fields = ['zero', 'one', 'two']
  a.fields = fields                # ONLY the Array 'a' is affected!
#
# keyword access is now allowed for many methods
#
  p a['zero']                        #=> 0
  p a['one']                         #=> 1
  p a['two']                         #=> 2
  p a.at('one')                      #=> 1
  p a.values_at('zero', 'two')       #=> [0, 2]
#
# assigmnet is allowed
#
  a['zero'] = 42
  p a['zero']                        #=> 42 
#
# assignment to non-fields results in the element being appended and the field
# being added for future use (also appended)
#
  p(a.fields.join(','))                 #=> "zero, one, two"
  p a['three']                          #=> nil
  a['three'] = 3
  p(a.fields.join(','))                 #=> "zero, one, two, three"
  p a['three']                          #=> 3 
#
# other detructive methods are also keyword enabled
#
  a.fill 42, 'zero', len = a.size
  p(a.values_at(a.fields))              #=> [42, 42, 42, 42]
  a.replace [0,1,2,3]

  a.slice! 'two', 2 
  p a                                   #=> [0,1] 

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/arrayfields-4.7.4/sample/a.rb
arrayfields-4.9.2 sample/a.rb
arrayfields-4.9.0 sample/a.rb
arrayfields-4.7.1 sample/a.rb
arrayfields-4.7.2 sample/a.rb
arrayfields-4.7.3 sample/a.rb
arrayfields-4.5.0 sample/a.rb
arrayfields-4.7.0 sample/a.rb
arrayfields-4.6.0 sample/a.rb
arrayfields-4.7.4 sample/a.rb