Association
General binary association allows one object to be associated with another. It has a variety of uses, link-lists, simple ordered maps and mixed collections, among them.
Usage
Associations can be used to draw simple relationships.
:Apple >> :Fruit :Apple >> :Red :Apple.associations #=> [ :Fruit, :Red ]
It can also be used for simple lists of ordered pairs.
c = [ :a >> 1, :b >> 2 ] c.each { |k,v| puts "#{k} associated with #{v} }
produces
a associated with 1 b associated with 2
Limitations
The method :>> is used to construct the association. It is a rarely used method so it is generally available. But you can‘t use an Association while extending any of the following classes becuase they use #>> for other things.
Bignum Fixnum Date IPAddr Process::Status
TODO: Should associations be singleton?
Methods
<=>
[]
inspect
invert!
new
reference
to_ary
to_s
Included Modules
- Comparable
Classes and Modules
Module Association::KernelAttributes
[RW] | index | |
[RW] | value |
Public Class methods
[ + ]
# File lib/more/facets/association.rb, line 110 def [](index, value) new(index, value) end
[ + ]
# File lib/more/facets/association.rb, line 126 def initialize(index, value=nil) @index = index @value = value unless index.associations.include?(value) index.associations << value end end
Store association references.
[ + ]
# File lib/more/facets/association.rb, line 106 def reference @reference ||= Hash.new{ |h,k,v| h[k]=[] } end
Public Instance methods
[ + ]
# File lib/more/facets/association.rb, line 135 def <=>(assoc) return -1 if self.value < assoc.value return 1 if self.value > assoc.value return 0 if self.value == assoc.value end
[ + ]
# File lib/more/facets/association.rb, line 151 def inspect %{#{@index.inspect} >> #{@value.inspect}} end
[ + ]
# File lib/more/facets/association.rb, line 141 def invert! temp = @index @index = @value @value = temp end
[ + ]
# File lib/more/facets/association.rb, line 155 def to_ary [ @index, @value ] end
[ + ]
# File lib/more/facets/association.rb, line 147 def to_s return "#{index.to_s}#{value.to_s}" end