Class: Wixy::Alphabet
- Inherits:
-
Object
- Object
- Wixy::Alphabet
- Defined in:
- lib/wixy/alphabet.rb
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#char(char, other_alphabet) ⇒ Object
Given a char and another alphabet, find the char in this alphabet at the corresponding index.
- #filtered(char) ⇒ Object
- #include?(char) ⇒ Boolean
- #index(char) ⇒ Object
-
#initialize(chars, options = {}) ⇒ Alphabet
constructor
chars is an array of character strings, like [“A”, “B”, “C” …].
- #sanitize(text_or_chars) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(chars, options = {}) ⇒ Alphabet
chars is an array of character strings, like [“A”, “B”, “C” …]
4 5 6 7 8 |
# File 'lib/wixy/alphabet.rb', line 4 def initialize(chars, = {}) @chars = chars.to_a.rotate([:shift] || 0) @index = Hash[@chars.each_with_index.to_a] @filters = [:filters] || [] end |
Instance Method Details
#[](index) ⇒ Object
10 11 12 |
# File 'lib/wixy/alphabet.rb', line 10 def [](index) @chars[index % @chars.length] end |
#char(char, other_alphabet) ⇒ Object
Given a char and another alphabet, find the char in this alphabet at the corresponding index
37 38 39 |
# File 'lib/wixy/alphabet.rb', line 37 def char(char, other_alphabet) self[other_alphabet.index(char)] end |
#filtered(char) ⇒ Object
22 23 24 |
# File 'lib/wixy/alphabet.rb', line 22 def filtered(char) @filters.inject(char) { |c, f| c.public_send(f) } end |
#include?(char) ⇒ Boolean
18 19 20 |
# File 'lib/wixy/alphabet.rb', line 18 def include?(char) index(filtered char) end |
#index(char) ⇒ Object
14 15 16 |
# File 'lib/wixy/alphabet.rb', line 14 def index(char) i = @index[filtered char] end |
#sanitize(text_or_chars) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/wixy/alphabet.rb', line 26 def sanitize(text_or_chars) chars = if text_or_chars.respond_to?(:chars) text_or_chars.chars else text_or_chars end chars.map {|c| filtered(c) }.map { |c| char_if_present(c) }.compact end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/wixy/alphabet.rb', line 41 def to_s "<Alphabet:[#{@chars.join}]>" end |