Skip to content

Instantly share code, notes, and snippets.

@peakpg
Created February 14, 2013 14:53
Show Gist options
  • Save peakpg/4953299 to your computer and use it in GitHub Desktop.
Save peakpg/4953299 to your computer and use it in GitHub Desktop.
# Implementing http://beust.com/weblog/2013/02/13/coding-challenge-light-edition in Ruby
# Contract for eql? and hash as per: http://stackoverflow.com/questions/2328685/how-to-make-object-instance-a-hash-key-in-ruby
class School
attr_accessor :name, :nickname
def initialize(name, nickname)
self.name = name
self.nickname = nickname
end
def eql?(other)
self.name == other.name || self.nickname == other.nickname
end
def hash()
[self.name, self.nickname].hash
end
end
require "test_helper"
class SchoolTest < ActiveSupport::TestCase
def setup
@school = School.new("James Madison University", "JMU" )
@identical_school = School.new("James Madison University", "JMU" )
@different_name = School.new("James Madison", "JMU")
@different_nick = School.new("James Madison University", "Dukes" )
@different_school = School.new("University of Virginia", "UVA" )
end
test "equality" do
assert @school.eql?(@identical_school)
assert @school.eql?(@different_name)
assert @school.eql?(@different_nick)
refute @school.eql?(@different_school)
end
test "Hash" do
assert_equal Fixnum, @school.hash.class
assert_equal @school.hash, @identical_school.hash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment