Skip to content

Instantly share code, notes, and snippets.

@bswinnerton
Created February 16, 2014 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bswinnerton/9038839 to your computer and use it in GitHub Desktop.
Save bswinnerton/9038839 to your computer and use it in GitHub Desktop.
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
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