Skip to content

Commit

Permalink
doc and fix delete_if
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Nov 29, 2009
1 parent 0d741d5 commit a33afce
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
35 changes: 31 additions & 4 deletions lib/multimap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,43 @@ def index(value)
invert[value]
end

def delete_if(&block) #:nodoc:
@hash.delete_if(&block)
# call-seq:
# map.delete_if {| key, value | block } -> map
#
# Deletes every key-value pair from <i>map</i> for which <i>block</i>
# evaluates to <code>true</code>.
#
# map = Multimap["a" => 100, "b" => [200, 300]]
# map.delete_if {|key, value| value >= 300 }
# #=> Multimap["a" => 100, "b" => 200]
#
def delete_if
each_association do |key, container|
container.delete_if do |value|
yield [key, value]
end
end
self
end

def reject(&block) #:nodoc:
# call-seq:
# map.reject {| key, value | block } -> map
#
# Same as <code>Multimap#delete_if</code>, but works on (and returns) a
# copy of the <i>map</i>. Equivalent to
# <code><i>map</i>.dup.delete_if</code>.
#
def reject(&block)
dup.delete_if(&block)
end

def reject!(&block) #:nodoc:
# call-seq:
# map.reject! {| key, value | block } -> map or nil
#
# Equivalent to <code>Multimap#delete_if</code>, but returns
# <code>nil</code> if no changes were made.
#
def reject!(&block)
old_size = size
delete_if(&block)
old_size == size ? nil : self
Expand Down
26 changes: 23 additions & 3 deletions spec/hash_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@
@map["b"].should eql(@container.new([300]))
end

it "should delete if condition is matched" do
it "should delete if key condition is matched" do
@map.delete_if { |key, value| key >= "b" }.should eql(@map)
@map["a"].should eql(@container.new([100]))
@map["b"].should eql(@container.new)

@map.delete_if { |key, value| key > "z" }.should eql(@map)
end

it "should delete if value condition is matched" do
@map.delete_if { |key, value| value >= 300 }.should eql(@map)
@map["a"].should eql(@container.new([100]))
@map["b"].should eql(@container.new([200]))
end

it "should duplicate the containers" do
map2 = @map.dup
map2.should_not equal(@map)
Expand Down Expand Up @@ -193,20 +199,34 @@
@map["c"].should eql(@container.new([300, 600]))
end

it "should reject key/value pairs on copy of the map" do
it "should reject key pairs on copy of the map" do
map = @map.reject { |key, value| key >= "b" }
map["b"].should eql(@container.new)
@map["b"].should eql(@container.new([200, 300]))
end

it "should reject key/value pairs" do
it "should reject value pairs on copy of the map" do
map = @map.reject { |key, value| value >= 300 }
map["b"].should eql(@container.new([200]))
@map["b"].should eql(@container.new([200, 300]))
end

it "should reject key pairs" do
@map.reject! { |key, value| key >= "b" }.should eql(@map)
@map["a"].should eql(@container.new([100]))
@map["b"].should eql(@container.new)

@map.reject! { |key, value| key >= "z" }.should eql(nil)
end

it "should reject value pairs" do
@map.reject! { |key, value| value >= 300 }.should eql(@map)
@map["a"].should eql(@container.new([100]))
@map["b"].should eql(@container.new([200]))

@map.reject! { |key, value| key >= "z" }.should eql(nil)
end

it "should select key/value pairs" do
@map.select { |k, v| k > "a" }.should eql(Multimap["b", [200, 300]])
@map.select { |k, v| v < 200 }.should eql(Multimap["a", 100])
Expand Down
16 changes: 5 additions & 11 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def <=>(other)
end
end

require 'forwardable'

class MiniArray
extend Forwardable

attr_accessor :data

def initialize(data = [])
Expand All @@ -41,17 +45,7 @@ def initialize_copy(orig)
@data = orig.data.dup
end

def <<(value)
@data << value
end

def each(&block)
@data.each(&block)
end

def delete(value)
@data.delete(value)
end
def_delegators :@data, :<<, :each, :delete, :delete_if

def ==(other)
other.is_a?(self.class) && @data == other.data
Expand Down

0 comments on commit a33afce

Please sign in to comment.