Module: Letterweight

Defined in:
lib/letterweight.rb

Class Method Summary collapse

Class Method Details

.length(string) ⇒ Object



2
3
4
5
6
7
# File 'lib/letterweight.rb', line 2

def self.length(string)
  char_array = string.split('')
  sum = char_array.inject(0) do |sum, l|
    sum + self::width(l)
  end
end

.width(letter) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/letterweight.rb', line 9

def self.width(letter)
  return self::length(letter) if letter.length > 1
  case letter
  when ''  then return 0.00
  when 'a' then return 0.67
  when 'b' then return 0.67
  when 'c' then return 0.67
  when 'd' then return 0.67
  when 'e' then return 0.67
  when 'f' then return 0.31
  when 'g' then return 0.67
  when 'h' then return 0.67
  when 'i' then return 0.26
  when 'j' then return 0.67
  when 'k' then return 0.67
  when 'l' then return 0.26
  when 'm' then return 1.00
  when 'n' then return 0.67
  when 'o' then return 0.67
  when 'p' then return 0.67
  when 'q' then return 0.67
  when 'r' then return 0.31
  when 's' then return 0.67
  when 't' then return 0.31
  when 'u' then return 0.67
  when 'v' then return 0.67
  when 'w' then return 0.80
  when 'x' then return 0.67
  when 'y' then return 0.67
  when 'z' then return 0.67
  when 'A' then return 0.80
  when 'B' then return 0.80
  when 'C' then return 0.80
  when 'D' then return 0.80
  when 'E' then return 0.80
  when 'F' then return 0.80
  when 'G' then return 0.80
  when 'H' then return 0.80
  when 'I' then return 0.31
  when 'J' then return 0.80
  when 'K' then return 0.31
  when 'L' then return 0.31
  when 'M' then return 1.00
  when 'N' then return 0.80
  when 'O' then return 0.80
  when 'P' then return 0.80
  when 'Q' then return 0.80
  when 'R' then return 0.80
  when 'S' then return 0.80
  when 'T' then return 0.80
  when 'U' then return 0.80
  when 'V' then return 0.80
  when 'W' then return 1.15
  when 'X' then return 0.80
  when 'Y' then return 0.80
  when 'Z' then return 0.80
  when ' ' then return 0.33
  when ',' then return 0.31
  when '.' then return 0.31
  when ';' then return 0.31
  when '?' then return 0.80
  when '@' then return 1.20
  when '$' then return 0.80
  when '%' then return 0.67
  when '^' then return 0.50
  when '*' then return 0.38
  when '(' then return 0.31
  when ')' then return 0.50
  when '&' then return 0.80
  when '!' then return 0.31
  when '-' then return 0.31
  when "'" then return 0.31
  when '"' then return 0.67
  when '/' then return 0.31
  when '|' then return 0.31
  when '`' then return 0.31
  when '~' then return 0.67
  when '[' then return 0.50
  when ']' then return 0.50
  when '{' then return 0.50
  when '}' then return 0.50
  when '=' then return 0.67
  when '+' then return 0.67
  else return 0.67
  end
end