1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
= Association
An Association is a class for creating simple pairings.
require 'hashery/association'
An Association can bew created through the usual means
of instantiation.
Association.new(:a, :b)
Or the shortcut method #>> can be used in most cases.
:x >> :z
An association provides two methods to access its content, #index and #value.
a = 'foo' >> 'bar'
a.index.assert == 'foo'
a.value.assert == 'bar'
Associations can be used to create ordered-hashes via normal
arrays.
keys = []
vals = []
ohash = [ 'A' >> '3', 'B' >> '2', 'C' >> '1' ]
ohash.each{ |k,v| keys << k ; vals << v }
keys.assert == ['A','B','C']
vals.assert == ['3','2','1']
Becuase Associations are objects in themselves more complex
collections can also be created.
complex = [
'parent' >> 'child',
'childless',
'another_parent' >> [
'subchildless',
'subparent' >> 'subchild'
]
]
An experimental feature of Association keeps a cache of all defined associations.
o = Object.new
o >> :a
o >> :b
o >> :c
o.associations.assert == [:a, :b, :c]
However this feature will probably be deprecated.
|