Class: ShiftCipher::Caeser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start = 0) ⇒ Caeser

Returns a new instance of Caeser.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shift_cipher/caeser.rb', line 5

def initialize(start = 0)

  # set default
  @offset = 0

  # have we got a string?
  if start.respond_to?(:ord)

    # is it alphabetic?
    if ('A'..'Z').include?(start)
      @offset = start.downcase.ord - 97
    elsif ('a'..'z').include?(start)
      @offset = start.ord - 97

    # is it numeric & in range?
    elsif (1..26).include?(start.to_i)
      @offset = start.to_i
    end

  # have we got a number
  elsif start.respond_to?(:to_i)
    # is it in range?
    if (1..26).include?(start.to_i)
      @offset = start.to_i
    end
  end

  # raise an error ... 
  # what about case when start set to 0?
end

Instance Attribute Details

#offsetObject

Returns the value of attribute offset.



3
4
5
# File 'lib/shift_cipher/caeser.rb', line 3

def offset
  @offset
end

Instance Method Details

#decrypt(message) ⇒ Object



40
41
42
# File 'lib/shift_cipher/caeser.rb', line 40

def decrypt(message)
  shift(message, - @offset)
end

#encrypt(message) ⇒ Object



36
37
38
# File 'lib/shift_cipher/caeser.rb', line 36

def encrypt(message)
  shift(message, @offset)
end