Class: Cryptogram::Mapper

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

Instance Method Summary collapse

Constructor Details

#initialize(alphabet:, shift:) ⇒ Mapper

Returns a new instance of Mapper.



3
4
5
6
7
8
# File 'lib/cryptogram/mapper.rb', line 3

def initialize(alphabet:, shift:)
  @shift = shift

  @mapper = build_mapper(alphabet)
  @swapcased_mapper = build_mapper(alphabet.join.swapcase.chars)
end

Instance Method Details

#map_to_initial(char) ⇒ Object



14
15
16
# File 'lib/cryptogram/mapper.rb', line 14

def map_to_initial(char)
  @mapper.key(char) || @swapcased_mapper.key(char) || char
end

#map_to_shifted(char) ⇒ Object



10
11
12
# File 'lib/cryptogram/mapper.rb', line 10

def map_to_shifted(char)
  @mapper.dig(char) || @swapcased_mapper.dig(char) || char
end