Class: Passweird::LeetSpeak

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

Overview

The LeetSpeak class is responsible for converting a given string into leet speak (1337 5p34k) and converting it back to normal text.

Example usage:

leet_speak = Passweird::LeetSpeak.new("example")
leet_string = leet_speak.leet
# => "3x4mpl3"
normal_string = leet_speak.unleet
# => "example"

Constant Summary collapse

ALPHABET_TO_SIMPLE_LEET =
{
  # Uppercase
  "A" => "4",
  "B" => "8",
  "C" => "(",
  "E" => "3",
  "F" => "ƒ",
  "G" => "6",
  "H" => "#",
  "I" => "1",
  "N" => "И",
  "O" => "0",
  "R" => "Я",
  "S" => "5",
  "T" => "7",
  "U" => "U",
  "Y" => "¥",
  "Z" => "2",
  # Downcase
  "a" => "4",
  "b" => "8",
  "c" => "(",
  "e" => "3",
  "f" => "ƒ",
  "g" => "6",
  "h" => "#",
  "i" => "1",
  "n" => "и",
  "o" => "0",
  "r" => "я",
  "s" => "5",
  "t" => "7",
  "u" => "u",
  "y" => "¥",
  "z" => "2"
}.freeze
LEET_TO_ALPHABET =

Reference: en.wikipedia.org/wiki/Leet#Table_of_leet-speak_substitutes_for_normal_letters Excluded leet speak equivalents that has 3 or more characters

{
  "4" => "A", "@" => "A", "Д" => "A",
  "8" => "B", "ß" => "B",
  "(" => "C", "{" => "C",
  "3" => "E", "£" => "E", "€" => "E",
  "ƒ" => "F",
  "6" => "G", "9" => "G",
  "#" => "H",
  "1" => "I", "!" => "I",
  "И" => "N", "ท" => "N",
  "0" => "O", "Ø" => "O",
  "Я" => "R",
  "5" => "S", "$" => "S",
  "7" => "T",
  "พ" => "W", "₩" => "W", "ω" => "W",
  "¥" => "Y",
  "2" => "Z"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_string) ⇒ LeetSpeak

Returns a new instance of LeetSpeak.

Raises:

  • (ArgumentError)


99
100
101
102
103
# File 'lib/passweird/leet_speak.rb', line 99

def initialize(given_string)
  raise ArgumentError, "given_string must be a String" unless given_string.is_a?(String)

  @given_string = given_string
end

Instance Attribute Details

#given_stringObject (readonly)

Returns the value of attribute given_string.



14
15
16
# File 'lib/passweird/leet_speak.rb', line 14

def given_string
  @given_string
end

Class Method Details

.leet(given_string) ⇒ Object



75
76
77
# File 'lib/passweird/leet_speak.rb', line 75

def self.leet(given_string)
  new(given_string).leet
end

.leet?(given_string) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/passweird/leet_speak.rb', line 79

def self.leet?(given_string)
  new(given_string).leet?
end

.leet_all(array_of_strings) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
# File 'lib/passweird/leet_speak.rb', line 83

def self.leet_all(array_of_strings)
  raise ArgumentError, "array_of_strings must be an Array" unless array_of_strings.is_a?(Array)

  array_of_strings.map { |string| leet(string) }
end

.unleet(given_string) ⇒ Object



89
90
91
# File 'lib/passweird/leet_speak.rb', line 89

def self.unleet(given_string)
  new(given_string).unleet
end

.unleet_all(array_of_strings) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
# File 'lib/passweird/leet_speak.rb', line 93

def self.unleet_all(array_of_strings)
  raise ArgumentError, "array_of_strings must be an Array" unless array_of_strings.is_a?(Array)

  array_of_strings.map { |string| unleet(string) }
end

Instance Method Details

#leetString

Converts the given_string to leet speak

Returns:

  • (String)

    the converted leet speak string



108
109
110
# File 'lib/passweird/leet_speak.rb', line 108

def leet
  given_string.gsub(/[#{ALPHABET_TO_SIMPLE_LEET.keys.join}]/, ALPHABET_TO_SIMPLE_LEET)
end

#leet?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/passweird/leet_speak.rb', line 112

def leet?
  given_string != leet
end

#unleetString

Converts the leet speak string back to normal text

Returns:

  • (String)

    the converted normal text string



119
120
121
# File 'lib/passweird/leet_speak.rb', line 119

def unleet
  given_string.upcase.gsub(/[#{LEET_TO_ALPHABET.keys.join}]/, LEET_TO_ALPHABET)
end