Created
February 16, 2014 18:49
-
-
Save bswinnerton/9038839 to your computer and use it in GitHub Desktop.
This file contains 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 'pry' | |
class MySet | |
attr_accessor :objects | |
def initialize | |
@objects = [] | |
end | |
def count | |
i = 0 | |
@objects.each do |object| | |
i += 1 | |
end | |
i | |
end | |
def add(*objects) | |
objects.each do |object| | |
@objects << object | |
end | |
@objects.uniq! | |
end | |
def union(set) | |
union_set = MySet.new | |
objects = (@objects + set.objects).uniq! | |
union_set.objects = objects | |
end | |
end |
This file contains 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_relative '../lib/set' | |
describe MySet do | |
before :each do | |
@set = MySet.new | |
end | |
it "is empty by default" do | |
expect(@set.count).to eq 0 | |
end | |
it "can add an object to the set" do | |
@set.add("Luna") | |
expect(@set.count).to eq 1 | |
end | |
it "can add multiple objects to the set" do | |
@set.add("Otto", "Luna") | |
expect(@set.count).to eq 2 | |
end | |
it "does not contain duplicates" do | |
@set.add("Otto") | |
@set.add("Otto") | |
expect(@set.count).to eq 1 | |
end | |
it "can combine two sets" do | |
set1 = MySet.new | |
set1.add("otto", "luna") | |
set2 = MySet.new | |
set2.add("otto", "mars") | |
unionset = set1.union(set2) | |
expect(unionset.count).to eq 3 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment