Last active
May 19, 2017 06:10
-
-
Save rio9791/a99a93ad67c1be0e1331f9fbb1362db6 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. | |
class Paddock | |
attr_accessor :lat, :lng, :name, :area | |
PRECISION = 4 | |
# | |
# redefine lat and lng as round value by 4 decimal places precision | |
# also leave name and area set by their value each as default | |
# | |
def initialize(lat:, lng:, name:, area:) | |
self.lat = equalizeValueOf lat | |
self.lng = equalizeValueOf lng | |
self.name = name | |
self.area = area | |
end | |
# | |
# redefine '==', by comparing each attributes with double equals method | |
# | |
def ==(other) | |
self.lat == equalizeValueOf(other.lat) && self.lng == equalizeValueOf(other.lng) && self.name == other.name && self.area == other.area | |
end | |
# | |
# redefine 'eql?', by comparing each attributes with 'eql?' method | |
# | |
def eql?(other) | |
self.lat.eql?(equalizeValueOf(other.lat)) && self.lng.eql?(equalizeValueOf(other.lng)) && self.name.eql?(other.name) && self.area.eql?(other.area) | |
end | |
# | |
# Override hash for check object uniq in array | |
# Ref: https://www.ruby-forum.com/topic/165653 | |
# | |
def hash | |
[lat, lng, name, area].hash | |
end | |
# | |
# checking the value of the lat and lng then redefine | |
# if the value is float, then it will make lat and lng value equal to rounded by 4 decimal places precision | |
# | |
def equalizeValueOf(value) | |
case value.class | |
when Fixnum then value | |
when Float then value.round(PRECISION).to_f | |
end | |
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