ruby-changes:11683
From: knu <ko1@a...>
Date: Fri, 1 May 2009 16:54:55 +0900 (JST)
Subject: [ruby-changes:11683] Ruby:r23322 (ruby_1_8): * lib/set.rb (SortedSet#add): Do not let an uncomparable object
knu 2009-05-01 16:54:42 +0900 (Fri, 01 May 2009) New Revision: 23322 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=23322 Log: * lib/set.rb (SortedSet#add): Do not let an uncomparable object in. [Bug #118] * lib/set.rb (Set#merge): Only directly use the passed objects @hash instance variable when self and the passed object are instances of the same class. [Bug #118] Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/lib/set.rb Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 23321) +++ ruby_1_8/ChangeLog (revision 23322) @@ -1,3 +1,12 @@ +Fri May 1 16:52:52 2009 Akinori MUSHA <knu@i...> + + * lib/set.rb (SortedSet#add): Do not let an uncomparable object + in. [Bug #118] + + * lib/set.rb (Set#merge): Only directly use the passed objects + @hash instance variable when self and the passed object are + instances of the same class. [Bug #118] + Fri May 1 16:01:31 2009 Nobuyoshi Nakada <nobu@r...> * configure.in: fixed the help strings for the header and library Index: ruby_1_8/lib/set.rb =================================================================== --- ruby_1_8/lib/set.rb (revision 23321) +++ ruby_1_8/lib/set.rb (revision 23322) @@ -20,7 +20,7 @@ # # The method +to_set+ is added to Enumerable for convenience. # -# See the Set class for an example of usage. +# See the Set and SortedSet documentation for examples of usage. # @@ -260,8 +260,8 @@ # Merges the elements of the given enumerable object to the set and # returns self. def merge(enum) - if enum.is_a?(Set) - @hash.update(enum.instance_eval { @hash }) + if enum.instance_of?(self.class) + @hash.update(enum.instance_variable_get(:@hash)) else enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable" enum.each { |o| add(o) } @@ -441,7 +441,35 @@ end end -# SortedSet implements a set which elements are sorted in order. See Set. +# +# SortedSet implements a Set that guarantees that it's element are +# yielded in sorted order (according to the return values of their +# #<=> methods) when iterating over them. +# +# All elements that are added to a SortedSet must include the +# Comparable module. +# +# Also, all elements must be <em>mutually comparable</em>: <tt>el1 <=> +# el2</tt> must not return <tt>nil</tt> for any elements <tt>el1</tt> +# and <tt>el2</tt>, else an ArgumentError will be raised when +# iterating over the SortedSet. +# +# == Example +# +# require "set" +# +# set = SortedSet.new(2, 1, 5, 6, 4, 5, 3, 3, 3) +# ary = [] +# +# set.each do |obj| +# ary << obj +# end +# +# p ary # => [1, 2, 3, 4, 5, 6] +# +# set2 = SortedSet.new(1, 2, "3") +# set2.each { |obj| } # => raises ArgumentError: comparison of Fixnum with String failed +# class SortedSet < Set @@setup = false @@ -466,6 +494,12 @@ @hash = RBTree.new super end + + def add(o) + o.is_a?(Comparable) or raise ArgumentError, "value must be comparable" + super + end + alias << add } rescue LoadError module_eval %{ @@ -485,9 +519,9 @@ end def add(o) + o.is_a?(Comparable) or raise ArgumentError, "value must be comparable" @keys = nil - @hash[o] = true - self + super end alias << add -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/