Module: ToRussianWords::Utils

Includes:
Divisions, UnderHundred
Included in:
ToRussianWords
Defined in:
lib/to_russian_words/utils.rb

Constant Summary

Constants included from Divisions

Divisions::DATIVE_DIVISIONS, Divisions::NOMINATIVE_DIVISIONS

Constants included from UnderHundred

ToRussianWords::UnderHundred::DATIVE_UNDER_HUNDRED, ToRussianWords::UnderHundred::NOMINATIVE_UNDER_HUNDRED

Instance Method Summary collapse

Instance Method Details

#check_sign(num) ⇒ Object



24
25
26
# File 'lib/to_russian_words/utils.rb', line 24

def check_sign(num)
  num < 0 ? [num.abs, 'negative '] : [num, '']
end

#divisions(russian_case) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/to_russian_words/utils.rb', line 43

def divisions(russian_case)
  case russian_case
  when 'dative'
    DATIVE_DIVISIONS
  else
    NOMINATIVE_DIVISIONS
  end
end

#higher_than_hundred(hundred, remaining, counter, russian_case) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/to_russian_words/utils.rb', line 16

def higher_than_hundred(hundred, remaining, counter, russian_case)
  century = (hundred == 1 ? '' : under_hundred(russian_case)[hundred])
  if remaining != 0
    return century + "#{hundred_name(russian_case, remaining)} " + under_hundred(russian_case)[remaining]
  end
  return century + "#{hundred_name(russian_case, remaining)} " if remaining == 0
end

#hundred_name(russian_case, remaining) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/to_russian_words/utils.rb', line 52

def hundred_name(russian_case, remaining)
  remaining = remaining.to_s.last.to_i
  case russian_case
  when 'dative'
    if remaining == 1
      'ста'
    else
      'сот'
    end
  else
    if remaining == 1
      'сто'
    elsif remaining.between?(2, 4)
      'ста'
    else
      'сот'
    end
  end
end

#numerical?(num) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/to_russian_words/utils.rb', line 28

def numerical?(num)
  Integer(num)
rescue
  raise 'A whole number is expected'
end

#result_below_one_thousand(num, counter, russian_case) ⇒ Object



9
10
11
12
13
14
# File 'lib/to_russian_words/utils.rb', line 9

def result_below_one_thousand(num, counter, russian_case)
  hundred, remaining = num.divmod(100)

  return higher_than_hundred(hundred, remaining, counter, russian_case) if hundred != 0
  under_hundred(russian_case)[remaining] if hundred == 0 && remaining != 0
end

#under_hundred(russian_case) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/to_russian_words/utils.rb', line 34

def under_hundred(russian_case)
  case russian_case
  when 'dative'
    DATIVE_UNDER_HUNDRED
  else
    NOMINATIVE_UNDER_HUNDRED
  end
end