Module: Num2words::Locales::RU

Defined in:
lib/num2words/locales/ru.rb

Constant Summary collapse

ONES_MASC =
I18n.t("num2words.ones_masc", locale: :ru)
ONES_FEM =
I18n.t("num2words.ones_fem", locale: :ru)
TEENS =
I18n.t("num2words.teens", locale: :ru)
TENS =
I18n.t("num2words.tens", locale: :ru)
HUNDREDS =
I18n.t("num2words.hundreds", locale: :ru)
SCALES =
I18n.t("num2words.scales", locale: :ru)
FRACTIONS =
I18n.t("num2words.fractions", locale: :ru)
GRAMMAR =
I18n.t("num2words.grammar", locale: :ru)
DATE =
I18n.t("num2words.date", locale: :ru)
DATE_TEMPLATE =
I18n.t("num2words.date.template", locale: :ru)
TIME =
I18n.t("num2words.time", locale: :ru)
TIME_TEMPLATE =
I18n.t("num2words.time.template", locale: :ru)
DATETIME_TEMPLATE =
I18n.t("num2words.datetime.template", locale: :ru)
ORDINALS =
I18n.t("num2words.numbers.ordinals", locale: :ru)

Class Method Summary collapse

Class Method Details

.cardinal(number, feminine: false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/num2words/locales/ru.rb', line 91

def cardinal(number, feminine: false)
  integer_value = Integer(number)
  negative = integer_value.negative?
  integer_value = integer_value.abs

  return (feminine ? ONES_FEM[0] : ONES_MASC[0]) if integer_value.zero?

  groups = integer_value.to_s
                        .chars.reverse.each_slice(3).map(&:reverse)
                        .map(&:join).map!(&:to_i).reverse

  words = []
  groups.each_with_index do |group_value, index|
    scale_idx = groups.size - index - 1
    group_feminine = feminine_group?(scale_idx) || feminine
    words.concat triple_to_words(group_value, scale_idx, feminine: group_feminine)
  end

  words.unshift(minus_word) if negative
  words.join(" ")
end

.date_day(day, format:, date_case:) ⇒ Object



64
65
66
67
# File 'lib/num2words/locales/ru.rb', line 64

def date_day(day, format:, date_case:)
  gender = date_case.to_sym == :genitive ? :masculine : :neuter
  ordinal(day, format, gender: gender)
end

.date_year(year, format:) ⇒ Object



69
70
71
# File 'lib/num2words/locales/ru.rb', line 69

def date_year(year, format:)
  ordinal(year, format)
end

.default_fraction_wordObject



42
43
44
# File 'lib/num2words/locales/ru.rb', line 42

def default_fraction_word
  GRAMMAR[:default_fraction]
end

.feminine_group?(scale_idx) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/num2words/locales/ru.rb', line 26

def feminine_group?(scale_idx)
  scale_idx == 1
end

.fraction_joiner(joiner) ⇒ Object



38
39
40
# File 'lib/num2words/locales/ru.rb', line 38

def fraction_joiner(joiner)
  joiner.to_sym == :and ? "и" : GRAMMAR[:conjunction]
end

.fraction_numerator_feminine?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/num2words/locales/ru.rb', line 30

def fraction_numerator_feminine?
  true
end

.minus_wordObject



34
35
36
# File 'lib/num2words/locales/ru.rb', line 34

def minus_word
  GRAMMAR[:minus]
end

.ordinal(value, format, gender: :masculine) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/num2words/locales/ru.rb', line 73

def ordinal(value, format, gender: :masculine)
  ordinals = ORDINALS[format]
  gender_data = ordinals[gender] || ordinals[:masculine]

  return gender_data[value - 1] if value.between?(1, gender_data.length)

  if value > 31
    thousands = (value / 100) * 100
    last_two = value % 100
    base_year = cardinal(thousands)
    last_ordinal = gender_data[last_two - 1] || cardinal(last_two)

    return [base_year, last_ordinal].join(" ")
  end

  cardinal(value)
end

.pluralize(number, singular, few, plural) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/num2words/locales/ru.rb', line 113

def pluralize(number, singular, few, plural)
  number = number.abs
  return plural if (11..14).include?(number % 100)

  case number % 10
  when 1 then singular
  when 2..4 then few
  else plural
  end
end

.triple_to_words(number, scale_idx, feminine: false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/num2words/locales/ru.rb', line 46

def triple_to_words(number, scale_idx, feminine: false)
  words = []

  words << HUNDREDS[number / 100] if number >= 100
  rest = number % 100

  if rest.between?(10, 19)
    words << TEENS[rest - 10]
  else
    words << TENS[rest / 10] if rest >= 20
    ones = rest % 10
    words << (feminine ? ONES_FEM[ones] : ONES_MASC[ones]) if ones.positive?
  end

  words << pluralize(number, *SCALES[scale_idx]) unless scale_idx.zero?
  words.compact
end