Skip to content

Commit

Permalink
Add set.rb to stdlib with basic specs
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Sep 21, 2013
1 parent 33902e1 commit bd5d97c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/opal/parser.rb
Expand Up @@ -1173,7 +1173,7 @@ def js_def(recvr, mid, args, stmts, line, end_line, sexp)

opt[1..-1].each do |o|
next if o[2][2] == :undefined
code << f("if (#{o[1]} == null) {\n#{@indent + INDENT}", o)
code << f("if (#{lvar_to_js o[1]} == null) {\n#{@indent + INDENT}", o)
code << process(o)
code << f("\n#{@indent}}", o)
end if opt
Expand Down
6 changes: 6 additions & 0 deletions spec/filters/bugs/set.rb
@@ -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
16 changes: 15 additions & 1 deletion spec/rubyspecs
Expand Up @@ -199,4 +199,18 @@ library/observer/delete_observer_spec
library/observer/delete_observers_spec
library/observer/notify_observers_spec
library/delegate/delegator/send_spec
library/erb/util/html_escape_spec
library/erb/util/html_escape_spec
library/set/initialize_spec
library/set/add_spec
library/set/append_spec
library/set/clear_spec
library/set/constructor_spec
library/set/each_spec
library/set/empty_spec
library/set/equal_value_spec
library/set/include_spec
library/set/length_spec
library/set/member_spec
library/set/merge_spec
library/set/size_spec
library/set/to_a_spec
83 changes: 83 additions & 0 deletions stdlib/set.rb
@@ -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

0 comments on commit bd5d97c

Please sign in to comment.