Class: Hashery::Association
- Includes:
- Comparable
- Defined in:
- lib/hashery/association.rb
Overview
This class is still fairly experimental. And it is not loaded along with the other Hashery libraries when using ‘require ’hashery’‘. It must be required independently.
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
The method :>> is used to construct the association. It is a rarely used method so it is generally available. But you can’t use it for any of the following classes becuase they use #>> for other things.
Bignum
Fixnum
Date
IPAddr
Process::Status
Association is a general binary association that allows one object to be associated with another. It has a variety of uses, such as linked-lists, simple ordered maps and mixed collections, among them.
Defined Under Namespace
Modules: Kernel
Instance Attribute Summary collapse
-
#index ⇒ Object
The “index key” of the association.
-
#value ⇒ Object
The “value” of the association.
Class Method Summary collapse
-
.[](index, value) ⇒ Object
Shortcut for #new.
-
.reference ⇒ Object
Store association references.
Instance Method Summary collapse
-
#<=>(assoc) ⇒ Integer
Compare the values of two associations.
-
#initialize(index, value = nil) ⇒ Association
constructor
Initialize new Association.
-
#inspect ⇒ String
Produce a literal code string for creating an association.
-
#invert! ⇒ Array
Invert association, making the index the value and vice-versa.
-
#to_ary ⇒ Array
Convert to two-element associative array.
-
#to_s ⇒ String
Produce a string representation.
Constructor Details
#initialize(index, value = nil) ⇒ Association
Initialize new Association.
94 95 96 97 98 99 100 101 |
# File 'lib/hashery/association.rb', line 94 def initialize(index, value=nil) @index = index @value = value unless index.associations.include?(value) index.associations << value end end |
Instance Attribute Details
#index ⇒ Object
The “index key” of the association.
81 82 83 |
# File 'lib/hashery/association.rb', line 81 def index @index end |
#value ⇒ Object
The “value” of the association.
86 87 88 |
# File 'lib/hashery/association.rb', line 86 def value @value end |
Class Method Details
.[](index, value) ⇒ Object
Shortcut for #new.
65 66 67 |
# File 'lib/hashery/association.rb', line 65 def [](index, value) new(index, value) end |
Instance Method Details
#<=>(assoc) ⇒ Integer
Compare the values of two associations.
112 113 114 115 116 |
# File 'lib/hashery/association.rb', line 112 def <=>(assoc) return -1 if self.value < assoc.value return 1 if self.value > assoc.value return 0 if self.value == assoc.value end |
#inspect ⇒ String
Produce a literal code string for creating an association.
143 144 145 |
# File 'lib/hashery/association.rb', line 143 def inspect "#{index.inspect} >> #{value.inspect}" end |
#invert! ⇒ Array
Invert association, making the index the value and vice-versa.
123 124 125 126 127 |
# File 'lib/hashery/association.rb', line 123 def invert! temp = @index @index = @value @value = temp end |
#to_ary ⇒ Array
Convert to two-element associative array.
152 153 154 |
# File 'lib/hashery/association.rb', line 152 def to_ary [index, value] end |
#to_s ⇒ String
Produce a string representation.
134 135 136 |
# File 'lib/hashery/association.rb', line 134 def to_s return "#{index} >> #{value}" end |