Class: Shuffle
- Inherits:
-
Object
- Object
- Shuffle
- Defined in:
- lib/shuffle.rb
Constant Summary collapse
- PREFIXES =
{ "mono" => 1, "di" => 2, "tri" => 3, "quad" => 4 }
Instance Attribute Summary collapse
-
#rejoin ⇒ Object
readonly
Returns the value of attribute rejoin.
-
#sequence ⇒ Object
readonly
Returns the value of attribute sequence.
Instance Method Summary collapse
-
#initialize(sequence) ⇒ Shuffle
constructor
A new instance of Shuffle.
- #shuffle(size) ⇒ Object
- #valid?(other_sequence, size) ⇒ Boolean
- #validate_random(size) ⇒ Object
Constructor Details
#initialize(sequence) ⇒ Shuffle
Returns a new instance of Shuffle.
13 14 15 16 17 18 19 20 |
# File 'lib/shuffle.rb', line 13 def initialize(sequence) @sequence = if sequence.is_a?(String) && sequence !~ /\s/ @rejoin = true sequence.split(//) else sequence end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
102 103 104 105 106 107 108 |
# File 'lib/shuffle.rb', line 102 def method_missing(name, *args, &block) if size = PREFIXES[name.to_s.match(/^(\w+)shuffle$/)[1]] shuffle(size.to_i) else super end end |
Instance Attribute Details
#rejoin ⇒ Object (readonly)
Returns the value of attribute rejoin.
11 12 13 |
# File 'lib/shuffle.rb', line 11 def rejoin @rejoin end |
#sequence ⇒ Object (readonly)
Returns the value of attribute sequence.
11 12 13 |
# File 'lib/shuffle.rb', line 11 def sequence @sequence end |
Instance Method Details
#shuffle(size) ⇒ Object
22 23 24 25 26 |
# File 'lib/shuffle.rb', line 22 def shuffle(size) shuffled_sequence = sequence.length > 1 ? shuffler(size) : sequence rejoin ? shuffled_sequence.join : shuffled_sequence end |
#valid?(other_sequence, size) ⇒ Boolean
28 29 30 31 32 |
# File 'lib/shuffle.rb', line 28 def valid?(other_sequence, size) split_other_sequence = other_sequence.is_a?(String) && sequence !~ /\s/ ? other_sequence.split(//) : other_sequence frequency(sequence.each_cons(size)) == frequency(split_other_sequence.each_cons(size)) end |
#validate_random(size) ⇒ Object
34 35 36 |
# File 'lib/shuffle.rb', line 34 def validate_random(size) valid?(shuffler(size), size) end |