Class: PermutationGenerator
- Inherits:
-
Object
- Object
- PermutationGenerator
- Defined in:
- lib/permutation_generator.rb
Overview
PermutationGenerator
Install
$ sudo gem install permutation_generator
Usage
require 'rubygems'
require 'permutation_generator'
PermutationGenerator.new(2, ["r", "b", "g"]).each do |element|
p element
end
# output -- ["r", "r"] ["r", "b"] ["r", "g"] ["b", "r"] ["b", "b"] ["b", "g"] ["g", "r"] ["g", "b"] ["g", "g"]
Copyright © 2010 MIT-LICENSE Author : Wayne Deng Web : blog.waynedeng.com Email : wayne.deng.cn(AT).com
Instance Attribute Summary collapse
-
#elements ⇒ Object
Returns the value of attribute elements.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #each_with_index(&block) ⇒ Object
-
#initialize(picking_size, elements) ⇒ PermutationGenerator
constructor
A new instance of PermutationGenerator.
- #permutation_size ⇒ Object
Constructor Details
#initialize(picking_size, elements) ⇒ PermutationGenerator
Returns a new instance of PermutationGenerator.
22 23 24 25 |
# File 'lib/permutation_generator.rb', line 22 def initialize(picking_size, elements) self.elements = elements self.size = picking_size end |
Instance Attribute Details
#elements ⇒ Object
Returns the value of attribute elements.
20 21 22 |
# File 'lib/permutation_generator.rb', line 20 def elements @elements end |
#size ⇒ Object
Returns the value of attribute size.
20 21 22 |
# File 'lib/permutation_generator.rb', line 20 def size @size end |
Instance Method Details
#each(&block) ⇒ Object
27 28 29 30 31 |
# File 'lib/permutation_generator.rb', line 27 def each (&block) (1..self.permutation_size).each do |i| yield self.ary(i-1) end end |
#each_with_index(&block) ⇒ Object
33 34 35 36 37 |
# File 'lib/permutation_generator.rb', line 33 def each_with_index (&block) (1..self.permutation_size).each do |i| yield self.ary(i-1), i-1 end end |
#permutation_size ⇒ Object
39 40 41 |
# File 'lib/permutation_generator.rb', line 39 def permutation_size self.elements.size ** self.size end |