Fri Feb 12 02:27:39 2010 Akinori MUSHA <knu@iDaemons.org>
* lib/set.rb (Set#initialize, Set#replace, Set#merge) (Set#subtract, Set#&): Fix duck type tests. [ruby-core:28078] * lib/set.rb (Set#initialize, Set#replace, Set#merge) (Set#subtract, Set#&): Try #each if #each_entry fails. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26648 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
36ff32040e
commit
e1d9adcc84
@ -1,3 +1,11 @@
|
|||||||
|
Fri Feb 12 02:27:39 2010 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* lib/set.rb (Set#initialize, Set#replace, Set#merge)
|
||||||
|
(Set#subtract, Set#&): Fix duck type tests. [ruby-core:28078]
|
||||||
|
|
||||||
|
* lib/set.rb (Set#initialize, Set#replace, Set#merge)
|
||||||
|
(Set#subtract, Set#&): Try #each if #each_entry fails.
|
||||||
|
|
||||||
Thu Feb 11 20:43:00 2010 Tanaka Akira <akr@fsij.org>
|
Thu Feb 11 20:43:00 2010 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* io.c (rb_io_oflags_modestr): return "r" for O_RDONLY|O_APPEND.
|
* io.c (rb_io_oflags_modestr): return "r" for O_RDONLY|O_APPEND.
|
||||||
@ -358,6 +366,7 @@ Tue Feb 2 14:30:27 2010 Yukihiro Matsumoto <matz@ruby-lang.org>
|
|||||||
for duck typing.
|
for duck typing.
|
||||||
|
|
||||||
* lib/set.rb (SortedSet#add): typo fixed.
|
* lib/set.rb (SortedSet#add): typo fixed.
|
||||||
|
|
||||||
Tue Feb 2 11:13:56 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Tue Feb 2 11:13:56 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* lib/delegate.rb (Delegator#marshal_dump): exclude
|
* lib/delegate.rb (Delegator#marshal_dump): exclude
|
||||||
|
26
lib/set.rb
26
lib/set.rb
@ -70,13 +70,23 @@ class Set
|
|||||||
enum.nil? and return
|
enum.nil? and return
|
||||||
|
|
||||||
if block
|
if block
|
||||||
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
|
do_with_enum(enum) { |o| add(block[o]) }
|
||||||
enum.each_entry { |o| add(block[o]) }
|
|
||||||
else
|
else
|
||||||
merge(enum)
|
merge(enum)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def do_with_enum(enum, &block)
|
||||||
|
if enum.respond_to?(:each_entry)
|
||||||
|
enum.each_entry(&block)
|
||||||
|
elsif enum.respond_to?(:each)
|
||||||
|
enum.each(&block)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "value must be enumerable"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
private :do_with_enum
|
||||||
|
|
||||||
# Copy internal hash.
|
# Copy internal hash.
|
||||||
def initialize_copy(orig)
|
def initialize_copy(orig)
|
||||||
@hash = orig.instance_eval{@hash}.dup
|
@hash = orig.instance_eval{@hash}.dup
|
||||||
@ -123,9 +133,8 @@ class Set
|
|||||||
if enum.class == self.class
|
if enum.class == self.class
|
||||||
@hash.replace(enum.instance_eval { @hash })
|
@hash.replace(enum.instance_eval { @hash })
|
||||||
else
|
else
|
||||||
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
|
|
||||||
clear
|
clear
|
||||||
enum.each_entry { |o| add(o) }
|
merge(enum)
|
||||||
end
|
end
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -281,8 +290,7 @@ class Set
|
|||||||
if enum.instance_of?(self.class)
|
if enum.instance_of?(self.class)
|
||||||
@hash.update(enum.instance_variable_get(:@hash))
|
@hash.update(enum.instance_variable_get(:@hash))
|
||||||
else
|
else
|
||||||
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
|
do_with_enum(enum) { |o| add(o) }
|
||||||
enum.each_entry { |o| add(o) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -291,8 +299,7 @@ class Set
|
|||||||
# Deletes every element that appears in the given enumerable object
|
# Deletes every element that appears in the given enumerable object
|
||||||
# and returns self.
|
# and returns self.
|
||||||
def subtract(enum)
|
def subtract(enum)
|
||||||
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
|
do_with_enum(enum) { |o| delete(o) }
|
||||||
enum.each_entry { |o| delete(o) }
|
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -314,9 +321,8 @@ class Set
|
|||||||
# Returns a new set containing elements common to the set and the
|
# Returns a new set containing elements common to the set and the
|
||||||
# given enumerable object.
|
# given enumerable object.
|
||||||
def &(enum)
|
def &(enum)
|
||||||
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
|
|
||||||
n = self.class.new
|
n = self.class.new
|
||||||
enum.each_entry { |o| n.add(o) if include?(o) }
|
do_with_enum(enum) { |o| n.add(o) if include?(o) }
|
||||||
n
|
n
|
||||||
end
|
end
|
||||||
alias intersection & ##
|
alias intersection & ##
|
||||||
|
Loading…
x
Reference in New Issue
Block a user