Class: Shufflino::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/shufflino.rb

Instance Method Summary collapse

Constructor Details

#initialize(seeds) ⇒ Core

Returns a new instance of Core.



10
11
12
13
14
15
16
17
18
19
# File 'lib/shufflino.rb', line 10

def initialize(seeds)
  if seeds.first.is_a? Array
    @seeds = seeds
  elsif seeds.first.is_a? String
    @seeds = seeds.map {|s| s.split("") }
  end

  raise "Empty seeds array" if seeds.empty?

end

Instance Method Details

#alphabet_sizeObject



43
44
45
# File 'lib/shufflino.rb', line 43

def alphabet_size
  @seeds.first.size
end

#combinations_sizeObject



39
40
41
# File 'lib/shufflino.rb', line 39

def combinations_size
  alphabet_size ** id_length
end

#generate(id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shufflino.rb', line 21

def generate(id)
  id = id.to_i
  raise Shufflino::Error.new 'Not enough seeds!' if id > combinations_size - 1

  indices = Bases[id.to_s].in_base(10).to_base(alphabet_size, array: true).map(&:to_i)

  indices = Array.new(id_length - indices.size, 0) + indices # rjust

  result = ""

  indices.each_with_index do |index, i|
    alphabet = @seeds[i]
    result << alphabet[index].to_s
  end

  result
end

#id_lengthObject



47
48
49
# File 'lib/shufflino.rb', line 47

def id_length
  @seeds.size
end