Created
October 25, 2012 01:37
-
-
Save dcrosby42/3949984 to your computer and use it in GitHub Desktop.
Example snippets for #ish blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assert_equal record.created_at, Time.now.ish | |
assert_equal bullet.velocity, 5.25.ish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@game_clock.tick | |
@space_ship.attrs.should == { x: 120, y: 0, rotation: 0 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@space_ship.attrs.should == { x: 120.ish, y: 0.ish, rotation: 0.ish } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
You have
def ish(acceptable_delta=0.001)
twice in numeric_ish.rb, otherwise an awesome idea.