Skip to content

Instantly share code, notes, and snippets.

@robolson
Created February 24, 2009 07:26
Show Gist options
  • Save robolson/69461 to your computer and use it in GitHub Desktop.
Save robolson/69461 to your computer and use it in GitHub Desktop.
require 'test/unit'
class Set
def initialize(initial=[])
@contents = Hash.new
initial.each do |item|
@contents[item.to_s] = item
end
return self
end
def <<(*items)
items.flatten.each do |item|
@contents[item.to_s] = item unless @contents.has_key?(item)
end
return self
end
def ==(set2)
@contents == set2.contents
end
protected
def contents
@contents
end
end
class SetTest < Test::Unit::TestCase
def setup
@s = Set.new([1, 2])
end
def test_only_add_once
@s << 1
assert_equal Set.new([1, 2]), @s
end
def test_add_multiple
@s << [9, 8]
assert_equal Set.new([1, 2, 8, 9]), @s
end
def test_has_no_order
assert_equal Set.new([1, 2]), Set.new([2, 1])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment