From: sawadatsuyoshi@... Date: 2014-10-26T00:38:59+00:00 Subject: [ruby-core:65898] [ruby-trunk - Feature #10425] [Open] A predicate method to tell if a number is near another Issue #10425 has been reported by Tsuyoshi Sawada. ---------------------------------------- Feature #10425: A predicate method to tell if a number is near another https://bugs.ruby-lang.org/issues/10425 * Author: Tsuyoshi Sawada * Status: Open * Priority: Normal * Assignee: * Category: * Target version: ---------------------------------------- A method `near?` like the following would be useful. ~~~ruby class Numeric def near? other, delta: Float::EPSILON (other.to_f - to_f).abs <= delta.to_f end end class Time def near? other, delta: Float::EPSILON to_f.near?(other.to_f, delta: delta) end end ~~~ It can be used to check errors, or whether something is around something. ~~~ruby 23.24324.near?(23.23, delta: 0.5) # => true t1 = t2 = Time.now t3 = Time.now t1.near?(t2) #=> true t1.near?(t3) #=> false 5.near?(3, delta: 1) #=> true ~~~ Some testing frameworks have something similar to this, but I think this is an elementary concept that Ruby should support at it core. -- https://bugs.ruby-lang.org/