Class: String

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

Overview

The String class

Instance Method Summary collapse

Instance Method Details

#rotn_shift(map) ⇒ Object

Shift a string by N symbols

Example:

>> "Hello World 123".encode({:a => 13, :n => 5})
=> "Uryyb Jbeyq 678"

Arguments:

map: (Hash)


13
14
15
16
17
18
# File 'lib/rotn.rb', line 13

def rotn_shift(map)
  string = self.dup
  string.tr!("a-zA-Z", Rotn.shift_dictionary(map[:a], :a)) if map.has_key? :a
  string.tr!("0-9", Rotn.shift_dictionary(map[:n], :n)) if map.has_key? :n
  return string
end

#rotn_shift!(map) ⇒ Object

In-place rotn_shift by N symbols

Example:

>> a = "Hello World 123"
=> "Hello World 123"
>> a.encode!({:a => 4, :n => 1})
=> "Lipps Asvph 234"
>> a
=> "Lipps Asvph 234"

Arguments:

map: (Hash)


31
32
33
# File 'lib/rotn.rb', line 31

def rotn_shift!(map)
  replace rotn_shift(map)
end

#rotn_unshift(map) ⇒ Object

Unrotn_shift a string by N symbols

Example:

>> "Yvccf Nficu 345".unrotn_shift({:a => 17, :n => 2})
=> "Hello World 123"


39
40
41
42
43
44
# File 'lib/rotn.rb', line 39

def rotn_unshift(map)
  string = self.dup
  string.tr!(Rotn.shift_dictionary(map[:a], :a), "a-zA-Z") if map.has_key? :a
  string.tr!(Rotn.shift_dictionary(map[:n], :n), "0-9") if map.has_key? :n
  return string
end

#rotn_unshift!(map) ⇒ Object

In-place unrotn_shift by N symbols

Example:

>> a = "Rovvy Gybvn 567"
=> "Rovvy Gybvn 567"
>> a.encode!({:a => 10, :n => 4})
=> "Hello World 123"
>> a
=> "Hello World 123"


54
55
56
# File 'lib/rotn.rb', line 54

def rotn_unshift!(map)
  replace unrotn_shift(map)
end