Skip to content

Instantly share code, notes, and snippets.

@luaysuarna
Last active May 10, 2017 01:29
Show Gist options
  • Save luaysuarna/7417ecf5c07913fa5b387011e99c30d5 to your computer and use it in GitHub Desktop.
Save luaysuarna/7417ecf5c07913fa5b387011e99c30d5 to your computer and use it in GitHub Desktop.
Paddock Equality
# 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, :modify_lat, :modify_lng
ATTR_NOT_COMPARED = [:lat, :lng]
def initialize(lat:, lng:, name:, area:)
self.lat = lat
self.lng = lng
self.name = name
self.area = area
self.modify_lat = precission lat
self.modify_lng = precission lng
end
#
# Make number precission with 4 decimal places
#
def precission(number, decimal_point = 4)
BigDecimal::new(number, 0).truncate(decimal_point).to_f
end
#
# Get Attributes
# eg: converting from :@lat to :lat
#
def attributes
instance_variables.map { |i| i.to_s.delete("@").to_sym }
end
#
# Get Attributes for compare value
# will remove unecessary attributes :lat, :lng
#
def attributes_compare
attributes.reject! { |attr| ATTR_NOT_COMPARED.include? attr }
end
#
# override compare with double equals
# eg: @paddock == @paddock2
#
def ==(paddock_compare)
attributes_compare.each { |attr| return false if self.send(attr) != paddock_compare.send(attr) }
return true
end
#
# override compare with eql?
# eg: @paddock.eql? @paddock2
#
def eql?(paddock_compare)
self == paddock_compare
end
#
# Override hash for check object uniq in array
# Ref: https://www.ruby-forum.com/topic/165653
#
def hash
[modify_lng, modify_lng, name, area].hash
end
end
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