Class: Approximately::DeltaFloat

Inherits:
Struct
  • Object
show all
Includes:
Comparable
Defined in:
lib/approximately.rb

Overview

This object can be used for float comparisons. When it is instantiaded with a float and a delta it will respond to <=>(another_float) and will return equality if the float it’s being compared to is within the delta

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#deltaObject

Returns the value of attribute delta

Returns:

  • (Object)

    the current value of delta



10
11
12
# File 'lib/approximately.rb', line 10

def delta
  @delta
end

#floatObject

Returns the value of attribute float

Returns:

  • (Object)

    the current value of float



10
11
12
# File 'lib/approximately.rb', line 10

def float
  @float
end

Instance Method Details

#<=>(another) ⇒ Object



17
18
19
20
21
# File 'lib/approximately.rb', line 17

def <=>(another)
  d = (to_f - another.to_f).abs
  return 0 if d < delta
  float <=> another.to_f
end

#inspectObject



23
24
25
# File 'lib/approximately.rb', line 23

def inspect
  "~%0.8f" % to_f
end

#to_fObject



13
14
15
# File 'lib/approximately.rb', line 13

def to_f
  float.to_f
end

#to_sObject



27
28
29
# File 'lib/approximately.rb', line 27

def to_s
  inspect
end