Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/my_scripts/extensions.rb,
lib/my_scripts/scripts/msdn/msdn_helper.rb

Constant Summary collapse

TRANSLIT_CYRILLIC =
%Q{АБВГДЕЁЗИЙКЛМНОПРСТУФЪЫЬЭабвгдеёзийклмнопрстуфъыьэ}
TRANSLIT_LATIN =
%Q{ABVGDEEZIJKLMNOPRSTUF"Y'Eabvgdeezijklmnoprstuf"y'e}
TRANSLIT_DOUBLES =
{'Ж'=>'ZH', 'Х'=>'KH', 'Ц'=>'TS', 'Ч'=>'CH', 'Ш'=>'SH', 'Щ'=>'SHCH', 'Ю'=>'YU', 'Я'=>'YA',
'ж'=>'zh', 'х'=>'kh', 'ц'=>'ts', 'ч'=>'ch', 'ш'=>'sh', 'щ'=>'shch', 'ю'=>'yu', 'я'=>'ya'}

Instance Method Summary collapse

Instance Method Details

#camel_caseObject

Turns string into CamelCase



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/my_scripts/extensions.rb', line 32

def camel_case
  if self.include? '_'
    self.split('_').map{|e| e.capitalize}.join
  else
    unless self =~ (/^[A-Z]/)
      self.capitalize
    else
      self
    end
  end
end

#snake_caseObject

Turns string into snake_case



27
28
29
# File 'lib/my_scripts/extensions.rb', line 27

def snake_case
  gsub(/([a-z])([A-Z0-9])/, '\1_\2' ).downcase
end

#to_classObject

Turns string into appropriate class constant, returns nil if class not found



17
18
19
20
21
22
23
24
# File 'lib/my_scripts/extensions.rb', line 17

def to_class
  klass = self.split("::").inject(Kernel) do |namespace, const|
    const == '' ? namespace : namespace.const_get(const)
  end
  klass.is_a?(Class) ? klass : nil
rescue NameError
  nil
end

#translit!Object

Performs basic transliteration from Cyrillic to Latin characters and standard character combinations



10
11
12
13
14
# File 'lib/my_scripts/extensions.rb', line 10

def translit!
  TRANSLIT_DOUBLES.each {|key, value| self.gsub!(key, value)}
  self.tr!(TRANSLIT_CYRILLIC, TRANSLIT_LATIN)
  self
end