Skip to content

Instantly share code, notes, and snippets.

@dcrosby42
Created October 25, 2012 01:37
Show Gist options
  • Save dcrosby42/3949984 to your computer and use it in GitHub Desktop.
Save dcrosby42/3949984 to your computer and use it in GitHub Desktop.
Example snippets for #ish blog post
assert_equal record.created_at, Time.now.ish
assert_equal bullet.velocity, 5.25.ish
class Numeric
def ish(acceptable_delta=0.001)
ApproximateValue.new self, acceptable_delta
end
end
class ApproximateValue
def initialize(me, acceptable_delta)
@me = me
@acceptable_delta = acceptable_delta
end
def ==(other)
(other - @me).abs < @acceptable_delta
end
def to_s
"within #{@acceptable_delta} of #{@me}"
end
end
@game_clock.tick
@space_ship.attrs.should == { x: 120, y: 0, rotation: 0 }
@space_ship.attrs.should == { x: 120.ish, y: 0.ish, rotation: 0.ish }
class Time
def ish
ApproxTime.new(self)
end
end
class ApproxTime
def initialize(t); @time = t; end
def ==(o)
return false if @time.nil? or o.nil?
(@time - o).abs < 5
end
end
@noahhendrix
Copy link

You have def ish(acceptable_delta=0.001) twice in numeric_ish.rb, otherwise an awesome idea.

@donv
Copy link

donv commented Dec 23, 2014

I have started using this now. @dcrosby42 what do you think about putting this in a gem?

gem 'equal-ish'

Easier to share across applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment