Module: ThaiNumerals

Defined in:
lib/thai_numerals.rb,
lib/thai_numerals/version.rb

Constant Summary collapse

THAI_NUMBERS =
{
  '0' => '',
  '1' => '',
  '2' => '',
  '3' => '',
  '4' => '',
  '5' => '',
  '6' => '',
  '7' => '',
  '8' => '',
  '9' => ''
}.freeze
THAI_COUNTINGS =
{
  '0' => 'ศูนย์',
  '1' => 'หนึ่ง',
  '2' => 'สอง',
  '3' => 'สาม',
  '4' => 'สี่',
  '5' => 'ห้า',
  '6' => 'หก',
  '7' => 'เจ็ด',
  '8' => 'แปด',
  '9' => 'เก้า'
}.freeze
THAI_SUFFIXS =
{
  0 => '',
  1 => 'สิบ',
  2 => 'ร้อย',
  3 => 'พัน',
  4 => 'หมื่น',
  5 => 'แสน',
  6 => 'ล้าน'
}.freeze
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.to_arabic(number) ⇒ Object



44
45
46
47
# File 'lib/thai_numerals.rb', line 44

def self.to_arabic(number)
  arabic_numbers = THAI_NUMBERS.invert
  number.to_s.gsub(Regexp.union(arabic_numbers.keys), arabic_numbers)
end

.to_thai(number) ⇒ Object



40
41
42
# File 'lib/thai_numerals.rb', line 40

def self.to_thai(number)
  number.to_s.gsub(Regexp.union(THAI_NUMBERS.keys), THAI_NUMBERS)
end

.to_thai_counting(number) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/thai_numerals.rb', line 49

def self.to_thai_counting(number)
  return THAI_COUNTINGS['0'] if number.zero?

  number.to_s.reverse.scan(/.{1,6}/).inject([]) do |arr, part|
    arr << thai_counting_partial(part)
  end.reverse.join(THAI_SUFFIXS[6])
end