Created
February 12, 2012 08:02
-
-
Save mahm/1807172 to your computer and use it in GitHub Desktop.
離散数学読書会(@2012-02-12)
This file contains hidden or 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
class MySet < Array | |
def and(set) | |
self.map{ |elem| elem if set.include? elem }.compact | |
end | |
def or(set) | |
result = self | |
set.each{ |elem| result << elem unless result.include? elem } | |
result | |
end | |
def minus(set) | |
self.map{ |elem| elem unless set.include? elem }.compact | |
end | |
def include?(elem) | |
self.each { |e| return true if e == elem } | |
nil | |
end | |
end | |
describe MySet do | |
describe "and" do | |
it { MySet.new([1, 2, 3]).and(MySet.new([2, 3])).should == [2, 3] } | |
end | |
describe "or" do | |
it { MySet.new([1, 2, 3]).or(MySet.new([2, 4, 5])).should == [1, 2, 3, 4, 5] } | |
end | |
describe "minus" do | |
it { MySet.new([1, 2, 3]).minus(MySet.new([2, 4, 5])).should == [1, 3] } | |
end | |
describe "include?" do | |
it { MySet.new([1, 2, 3]).include?(2).should be_true } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment