Class: CldrPlurals::RubyRuntime::StrNum
- Inherits:
-
Object
- Object
- CldrPlurals::RubyRuntime::StrNum
- Defined in:
- lib/cldr-plurals/ruby-runtime/str_num.rb
Instance Attribute Summary collapse
-
#exp ⇒ Object
readonly
Returns the value of attribute exp.
-
#frac ⇒ Object
readonly
Returns the value of attribute frac.
-
#int ⇒ Object
readonly
Returns the value of attribute int.
-
#sign ⇒ Object
readonly
Returns the value of attribute sign.
Class Method Summary collapse
Instance Method Summary collapse
- #abs ⇒ Object
- #apply_exp ⇒ Object
- #frac_val ⇒ Object
-
#initialize(sign, int, frac, exp) ⇒ StrNum
constructor
A new instance of StrNum.
- #int_val ⇒ Object
- #strip ⇒ Object
- #to_s ⇒ Object
- #to_val ⇒ Object
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
#exp ⇒ Object (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 |
#frac ⇒ Object (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 |
#int ⇒ Object (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 |
#sign ⇒ Object (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
#abs ⇒ Object
38 39 40 |
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 38 def abs self.class.new('', int, frac, exp) end |
#apply_exp ⇒ Object
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_val ⇒ Object
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_val ⇒ Object
19 20 21 |
# File 'lib/cldr-plurals/ruby-runtime/str_num.rb', line 19 def int_val int.to_i end |
#strip ⇒ Object
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_s ⇒ Object
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_val ⇒ Object
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 |