Class: CldrPlurals::RubyRuntime::StrNum

Inherits:
Object
  • Object
show all
Defined in:
lib/cldr-plurals/ruby-runtime/str_num.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sign, int, frac, exp) ⇒ StrNum

Returns a new instance of StrNum.



12
13
14
15
16
17
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 12

def initialize(sign, int, frac, exp)
  @sign = sign
  @int = int
  @frac = frac
  @exp = exp
end

Instance Attribute Details

#expObject (readonly)

Returns the value of attribute exp.



10
11
12
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 10

def exp
  @exp
end

#fracObject (readonly)

Returns the value of attribute frac.



10
11
12
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 10

def frac
  @frac
end

#intObject (readonly)

Returns the value of attribute int.



10
11
12
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 10

def int
  @int
end

#signObject (readonly)

Returns the value of attribute sign.



10
11
12
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 10

def sign
  @sign
end

Class Method Details

.from_string(str) ⇒ Object



5
6
7
8
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 5

def self.from_string(str)
  sign, int, frac, exp = str.scan(/([+-])?(\d+)\.?(\d+)?[eEcC]?(-?\d+)?/).flatten
  new(sign || '', int, frac || '', exp.to_i)
end

Instance Method Details

#absObject



38
39
40
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 38

def abs
  self.class.new('', int, frac, exp)
end

#apply_expObject



28
29
30
31
32
33
34
35
36
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 28

def apply_exp
  new_int, new_frac = if exp > 0
    shift_right(exp)
  else
    shift_left(exp.abs)
  end

  self.class.new(sign, new_int || '', new_frac || '', 0)
end

#frac_valObject



23
24
25
26
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 23

def frac_val
  # remove leading zeroes
  frac.sub(/\A0*/, '')
end

#int_valObject



19
20
21
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 19

def int_val
  int.to_i
end

#stripObject



50
51
52
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 50

def strip
  self.class.new(sign, int, frac.sub(/0+\z/, ''), exp)
end

#to_sObject



42
43
44
45
46
47
48
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 42

def to_s
  ''.tap do |result|
    result << "#{sign}#{int}"
    result << ".#{frac}" unless frac.empty?
    result << "e#{exp}" if exp != 0
  end
end

#to_valObject



54
55
56
57
58
59
60
61
62
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 54

def to_val
  str = to_s

  if str.include?('.')
    str.to_f
  else
    str.to_i * (10 ** exp)
  end
end