Module: KeySwitcher

Extended by:
KeySwitcher
Included in:
KeySwitcher
Defined in:
lib/key_switcher.rb

Instance Method Summary collapse

Instance Method Details

#switch(str) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/key_switcher.rb', line 6

def switch(str)

  ru = "йцукенгшщзхъфывапролджэячсмитьбюёЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮЁ".split('')
  en = "qwertyuiop[]asdfghjkl;'zxcvbnm,.`QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>~".split('')

  return str unless letters = str.match(/([^\W\d_])/)

  first_letter = letters[0]
  mapping = first_letter.in?(ru) ? Hash[ru.zip(en)] : Hash[en.zip(ru)]

  str.split('').inject('') { |result, char| result << (mapping[char] || char) }

end