Last active
May 10, 2017 07:20
-
-
Save nurhadimaulana2309/d835c2540738334abe3dbbbad5845751 to your computer and use it in GitHub Desktop.
Paddock Equality
This file contains hidden or 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
# In this example case when we create paddock objects we want them to be equal | |
# to each other if their name and area are the same and their lat/lng are "close | |
# enough" to each other. In our case we want "close enough" to mean the lat and | |
# lng match up to four decimal places. | |
# | |
# Download this file and the corresponding test case file tc_paddock.rb. Run the | |
# test case file like this `ruby tc_paddock.rb`. Currently all tests fail. | |
# Please make the changes you feel are necessary to make the tests pass, then | |
# make a gist with your version of paddock.rb and let us know. | |
require 'bigdecimal' | |
class Paddock | |
attr_accessor :lat, :lng, :name, :area | |
PRECISION = 4 | |
# | |
# lat and lng attribute get shortened when initializing | |
# | |
def initialize(lat:, lng:, name:, area:) | |
self.lat = shortDecimalOf lat | |
self.lng = shortDecimalOf lng | |
self.name = name | |
self.area = area | |
end | |
# | |
# redefine '==' method with custom behaviour | |
# by comparing object attributes - NOT by object_id anymore | |
# | |
def ==(other_paddock) | |
self.name == other_paddock.name and | |
self.area == other_paddock.area and | |
self.lat == shortDecimalOf(other_paddock.lat) and | |
self.lng == shortDecimalOf(other_paddock.lng) | |
end | |
# | |
# redefine 'eql?' method with redefined '==' method above | |
# | |
alias eql? == | |
def hash | |
[lat, lng, name, area].hash | |
end | |
# | |
# short out value to 4 decimal precision | |
# | |
def shortDecimalOf(value) | |
BigDecimal::new(value, 0).truncate(PRECISION).to_f | |
end | |
end |
This file contains hidden or 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
require 'test/unit' | |
require_relative 'paddock' | |
class TestPaddock < Test::Unit::TestCase | |
def setup | |
@p1 = Paddock.new(lat: -31.9415, lng: 115.8418, name: 'Agworld HQ', area: 10) | |
@p2 = Paddock.new(lat: -31.94154, lng: 115.84183, name: 'Agworld HQ', area: 10) | |
@p3 = Paddock.new(lat: -31.9405, lng: 115.8408, name: 'Agworld HQ', area: 11) | |
@p4 = Paddock.new(lat: -31.9415, lng: 115.8418, name: 'Agworld Branch', area: 10) | |
end | |
def test_double_equals | |
assert(@p1 == @p2, 'p1 should == p2') | |
assert_equal(@p1 == @p3, false, 'p1 should not == p3') | |
assert_equal(@p1 == @p4, false, 'p1 should not == p4') | |
end | |
def test_eql? | |
assert(@p1.eql?(@p2), 'p1 should eql? p2') | |
assert_equal(@p1.eql?(@p3), false, 'p1 should not eql? p3') | |
end | |
def test_uniq | |
assert_equal([@p1, @p2].uniq.length, 1) | |
assert_equal([@p1, @p3].uniq.length, 2) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment