Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add set.rb to stdlib with basic specs
- Loading branch information
1 parent
33902e1
commit bd5d97c
Showing
4 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
opal_filter "Set" do | ||
fails "Set#initialize is private" | ||
fails "Set#== returns true when the passed Object is a Set and self and the Object contain the same elements" | ||
fails "Set#== does not depend on the order of nested Sets" | ||
fails "Set#merge raises an ArgumentError when passed a non-Enumerable" | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
class Set | ||
include Enumerable | ||
|
||
def self.[](*ary) | ||
new(ary) | ||
end | ||
|
||
def initialize(enum = nil, &block) | ||
@hash = Hash.new | ||
|
||
return if enum.nil? | ||
|
||
if block | ||
do_with_enum(enum) { |o| add(block[o]) } | ||
else | ||
merge(enum) | ||
end | ||
end | ||
|
||
def ==(other) | ||
if self.equal?(other) | ||
true | ||
elsif other.instance_of?(self.class) | ||
@hash == other.instance_variable_get(:@hash) | ||
elsif other.is_a?(Set) && self.size == other.size | ||
other.all? { |o| @hash.include?(o) } | ||
else | ||
false | ||
end | ||
end | ||
|
||
def add(o) | ||
@hash[o] = true | ||
self | ||
end | ||
alias << add | ||
|
||
def add?(o) | ||
if include?(o) | ||
nil | ||
else | ||
add(o) | ||
end | ||
end | ||
|
||
def each(&block) | ||
return enum_for :each unless block_given? | ||
@hash.each_key(&block) | ||
self | ||
end | ||
|
||
def empty? | ||
@hash.empty? | ||
end | ||
|
||
def clear | ||
@hash.clear | ||
self | ||
end | ||
|
||
def include?(o) | ||
@hash.include?(o) | ||
end | ||
alias member? include? | ||
|
||
def merge(enum) | ||
do_with_enum(enum) { |o| add o } | ||
self | ||
end | ||
|
||
def do_with_enum(enum, &block) | ||
enum.each(&block) | ||
end | ||
|
||
def size | ||
@hash.size | ||
end | ||
alias length size | ||
|
||
def to_a | ||
@hash.keys | ||
end | ||
end |