Class: Rjq::Number

Inherits:
Numeric
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(literal) ⇒ Number

Returns a new instance of Number.

Raises:

  • (ArgumentError)


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

#literalObject (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

#absObject



84
# File 'lib/rjq/number.rb', line 84

def abs = @value.abs

#ceilObject



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

#dumpObject



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

Returns:

  • (Boolean)


62
63
64
# File 'lib/rjq/number.rb', line 62

def eql?(other)
  other.is_a?(Numeric) && self == other
end

#finite?Boolean

Returns:

  • (Boolean)


89
# File 'lib/rjq/number.rb', line 89

def finite? = @value.finite?

#floorObject



86
# File 'lib/rjq/number.rb', line 86

def floor = @value.floor

#hashObject



66
67
68
# File 'lib/rjq/number.rb', line 66

def hash
  @value.hash
end

#infinite?Boolean

Returns:

  • (Boolean)


90
# File 'lib/rjq/number.rb', line 90

def infinite? = @value.infinite?

#inspectObject



46
47
48
# File 'lib/rjq/number.rb', line 46

def inspect
  "#<#{self.class} #{literal}>"
end

#nan?Boolean

Returns:

  • (Boolean)


91
# File 'lib/rjq/number.rb', line 91

def nan? = @value.nan?

#negative?Boolean

Returns:

  • (Boolean)


92
# File 'lib/rjq/number.rb', line 92

def negative? = @value.negative?

#positive?Boolean

Returns:

  • (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))

#roundObject



87
# File 'lib/rjq/number.rb', line 87

def round(...) = @value.round(...)

#to_fObject



30
31
32
# File 'lib/rjq/number.rb', line 30

def to_f
  @value
end

#to_iObject



34
35
36
# File 'lib/rjq/number.rb', line 34

def to_i
  @value.to_i
end

#to_intObject



38
39
40
# File 'lib/rjq/number.rb', line 38

def to_int
  to_i
end

#to_sObject



42
43
44
# File 'lib/rjq/number.rb', line 42

def to_s
  dump
end

#truncateObject



88
# File 'lib/rjq/number.rb', line 88

def truncate = @value.truncate

#zero?Boolean

Returns:

  • (Boolean)


94
# File 'lib/rjq/number.rb', line 94

def zero? = @value.zero?