Class: Rjq::Number
- Inherits:
-
Numeric
- Object
- Numeric
- Rjq::Number
- Includes:
- Comparable
- Defined in:
- lib/rjq/number.rb
Constant Summary collapse
- NUMBER_PATTERN =
/\A-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?\z/
Instance Attribute Summary collapse
-
#literal ⇒ Object
readonly
Returns the value of attribute literal.
Class Method Summary collapse
Instance Method Summary collapse
- #%(other) ⇒ Object
- #*(other) ⇒ Object
- #**(other) ⇒ Object
- #+(other) ⇒ Object
- #+@ ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #abs ⇒ Object
- #ceil ⇒ Object
- #coerce(other) ⇒ Object
- #decimal_compare(other) ⇒ Object
- #dump ⇒ Object
- #eql?(other) ⇒ Boolean
- #finite? ⇒ Boolean
- #floor ⇒ Object
- #hash ⇒ Object
- #infinite? ⇒ Boolean
-
#initialize(literal) ⇒ Number
constructor
A new instance of Number.
- #inspect ⇒ Object
- #nan? ⇒ Boolean
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
- #remainder(other) ⇒ Object
- #round ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_int ⇒ Object
- #to_s ⇒ Object
- #truncate ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(literal) ⇒ Number
Returns a new instance of Number.
15 16 17 18 19 20 21 22 |
# File 'lib/rjq/number.rb', line 15 def initialize(literal) @literal = literal.to_s.freeze raise ArgumentError, 'invalid number literal' unless NUMBER_PATTERN.match?(@literal) @value = Float(@literal) parse_decimal_components freeze end |
Instance Attribute Details
#literal ⇒ Object (readonly)
Returns the value of attribute literal.
9 10 11 |
# File 'lib/rjq/number.rb', line 9 def literal @literal end |
Class Method Details
.parse(literal) ⇒ Object
11 12 13 |
# File 'lib/rjq/number.rb', line 11 def self.parse(literal) new(literal) end |
Instance Method Details
#%(other) ⇒ Object
78 |
# File 'lib/rjq/number.rb', line 78 def %(other) = @value % numeric_value(other) |
#*(other) ⇒ Object
76 |
# File 'lib/rjq/number.rb', line 76 def *(other) = @value * numeric_value(other) |
#**(other) ⇒ Object
79 |
# File 'lib/rjq/number.rb', line 79 def **(other) = @value**numeric_value(other) |
#+(other) ⇒ Object
74 |
# File 'lib/rjq/number.rb', line 74 def +(other) = @value + numeric_value(other) |
#+@ ⇒ Object
82 |
# File 'lib/rjq/number.rb', line 82 def +@ = @value |
#-(other) ⇒ Object
75 |
# File 'lib/rjq/number.rb', line 75 def -(other) = @value - numeric_value(other) |
#-@ ⇒ Object
81 |
# File 'lib/rjq/number.rb', line 81 def -@ = -@value |
#/(other) ⇒ Object
77 |
# File 'lib/rjq/number.rb', line 77 def /(other) = @value / numeric_value(other) |
#<=>(other) ⇒ Object
50 51 52 53 54 |
# File 'lib/rjq/number.rb', line 50 def <=>(other) return decimal_compare(other) if other.is_a?(Number) @value <=> numeric_value(other) end |
#==(other) ⇒ Object
56 57 58 59 60 |
# File 'lib/rjq/number.rb', line 56 def ==(other) return decimal_compare(other).zero? if other.is_a?(Number) other.is_a?(Numeric) && @value == numeric_value(other) end |
#abs ⇒ Object
84 |
# File 'lib/rjq/number.rb', line 84 def abs = @value.abs |
#ceil ⇒ Object
85 |
# File 'lib/rjq/number.rb', line 85 def ceil = @value.ceil |
#coerce(other) ⇒ Object
70 71 72 |
# File 'lib/rjq/number.rb', line 70 def coerce(other) [numeric_value(other), @value] end |
#decimal_compare(other) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rjq/number.rb', line 96 def decimal_compare(other) sign_comparison = @decimal_sign <=> other.instance_variable_get(:@decimal_sign) return sign_comparison unless sign_comparison.zero? return 0 if @decimal_sign.zero? other_digits = other.instance_variable_get(:@decimal_digits) other_exponent = other.instance_variable_get(:@decimal_exponent) magnitude_comparison = (@decimal_digits.length + @decimal_exponent) <=> (other_digits.length + other_exponent) return magnitude_comparison * @decimal_sign unless magnitude_comparison.zero? coefficient_comparison = compare_coefficients(@decimal_digits, other_digits) coefficient_comparison * @decimal_sign end |
#dump ⇒ Object
24 25 26 27 28 |
# File 'lib/rjq/number.rb', line 24 def dump return literal unless literal.match?(/[eE]/) render_exponent_literal end |
#eql?(other) ⇒ Boolean
62 63 64 |
# File 'lib/rjq/number.rb', line 62 def eql?(other) other.is_a?(Numeric) && self == other end |
#finite? ⇒ Boolean
89 |
# File 'lib/rjq/number.rb', line 89 def finite? = @value.finite? |
#floor ⇒ Object
86 |
# File 'lib/rjq/number.rb', line 86 def floor = @value.floor |
#hash ⇒ Object
66 67 68 |
# File 'lib/rjq/number.rb', line 66 def hash @value.hash end |
#infinite? ⇒ Boolean
90 |
# File 'lib/rjq/number.rb', line 90 def infinite? = @value.infinite? |
#inspect ⇒ Object
46 47 48 |
# File 'lib/rjq/number.rb', line 46 def inspect "#<#{self.class} #{literal}>" end |
#nan? ⇒ Boolean
91 |
# File 'lib/rjq/number.rb', line 91 def nan? = @value.nan? |
#negative? ⇒ Boolean
92 |
# File 'lib/rjq/number.rb', line 92 def negative? = @value.negative? |
#positive? ⇒ Boolean
93 |
# File 'lib/rjq/number.rb', line 93 def positive? = @value.positive? |
#remainder(other) ⇒ Object
80 |
# File 'lib/rjq/number.rb', line 80 def remainder(other) = @value.remainder(numeric_value(other)) |
#round ⇒ Object
87 |
# File 'lib/rjq/number.rb', line 87 def round(...) = @value.round(...) |
#to_f ⇒ Object
30 31 32 |
# File 'lib/rjq/number.rb', line 30 def to_f @value end |
#to_i ⇒ Object
34 35 36 |
# File 'lib/rjq/number.rb', line 34 def to_i @value.to_i end |
#to_int ⇒ Object
38 39 40 |
# File 'lib/rjq/number.rb', line 38 def to_int to_i end |
#to_s ⇒ Object
42 43 44 |
# File 'lib/rjq/number.rb', line 42 def to_s dump end |
#truncate ⇒ Object
88 |
# File 'lib/rjq/number.rb', line 88 def truncate = @value.truncate |
#zero? ⇒ Boolean
94 |
# File 'lib/rjq/number.rb', line 94 def zero? = @value.zero? |