Class: Set::Combination
- Defined in:
- lib/set/combination/version.rb,
lib/set/combination.rb
Constant Summary collapse
- VERSION =
"0.0.1"- InvalidDimentionError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#dimentions ⇒ Object
readonly
Returns the value of attribute dimentions.
Instance Method Summary collapse
- #*(dimention) ⇒ Object
-
#initialize(*dimentions) ⇒ Combination
constructor
A new instance of Combination.
- #inspect ⇒ Object
Constructor Details
#initialize(*dimentions) ⇒ Combination
Returns a new instance of Combination.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/set/combination.rb', line 11 def initialize *dimentions @dimentions = dimentions.clone unless dimentions.all?{|dimention| dimention.respond_to? :to_a } raise Set::Combination::InvalidDimentionError.new "all dimentions must respond to to_a" end members = dimentions.map(&:to_a).flatten if members != members.uniq raise Set::Combination::InvalidDimentionError.new "all members in a combination must be uniq" end combinations = dimentions.shift.to_a.map{|v| Set[v] }.to_set combinations = dimentions.inject(combinations){ |combinations, dimention| combinations.inject([]){|new_combinations, combination| (new_combinations + dimention.to_a.map{ |member| combination + Set[member] }).to_set } } super combinations end |
Instance Attribute Details
#dimentions ⇒ Object (readonly)
Returns the value of attribute dimentions.
32 33 34 |
# File 'lib/set/combination.rb', line 32 def dimentions @dimentions end |
Instance Method Details
#*(dimention) ⇒ Object
34 35 36 37 |
# File 'lib/set/combination.rb', line 34 def * dimention dimentions = dimention.respond_to?(:dimentions) ? dimention.dimentions : [dimention] self.class.new *(self.dimentions + dimentions) end |
#inspect ⇒ Object
39 40 41 |
# File 'lib/set/combination.rb', line 39 def inspect %(#<#{self.class} #{to_a.map(&:inspect)*', '}>) end |