ruby-changes:29999
From: knu <ko1@a...>
Date: Fri, 19 Jul 2013 11:22:22 +0900 (JST)
Subject: [ruby-changes:29999] knu:r42051 (trunk): Define Set#to_set so that aSet.to_set returns self.
knu 2013-07-19 11:22:11 +0900 (Fri, 19 Jul 2013) New Revision: 42051 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42051 Log: Define Set#to_set so that aSet.to_set returns self. * lib/set.rb (Set#to_set): Define Set#to_set so that aSet.to_set returns self. [Fixes GH-359] Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/set.rb trunk/test/test_set.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 42050) +++ ChangeLog (revision 42051) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Jul 19 11:16:54 2013 Akinori MUSHA <knu@i...> + + * lib/set.rb (Set#to_set): Define Set#to_set so that aSet.to_set + returns self. [Fixes GH-359] + Fri Jul 19 11:10:23 2013 Zachary Scott <e@z...> * lib/rake/*: [DOC] Capitalize "Ruby" in documentation Index: lib/set.rb =================================================================== --- lib/set.rb (revision 42050) +++ lib/set.rb (revision 42051) @@ -148,6 +148,16 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L148 @hash.keys end + # Returns self if no arguments are given. Otherwise, converts the + # set to another with klass.new(self, *args, &block). + # + # In subclasses, returns klass.new(self, *args, &block) unless + # overridden. + def to_set(klass = Set, *args, &block) + return self if instance_of?(Set) && klass == Set && block.nil? && args.empty? + klass.new(self, *args, &block) + end + def flatten_merge(set, seen = Set.new) # :nodoc: set.each { |e| if e.is_a?(Set) Index: NEWS =================================================================== --- NEWS (revision 42050) +++ NEWS (revision 42051) @@ -137,6 +137,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L137 === Stdlib compatibility issues (excluding feature bug fixes) +* Set + * incompatible changes: + * Set#to_set now returns self instead of generating a copy. + * URI * incompatible changes: * URI.decode_www_form follows current WHATWG URL Standard. Index: test/test_set.rb =================================================================== --- test/test_set.rb (revision 42050) +++ test/test_set.rb (revision 42051) @@ -625,6 +625,9 @@ class TC_Enumerable < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L625 assert_instance_of(Set, set) assert_equal([-10,-8,-6,-4,-2], set.sort) + assert_same set, set.to_set + assert_not_same set, set.to_set { |o| o } + set = ary.to_set(SortedSet) assert_instance_of(SortedSet, set) assert_equal([1,2,3,4,5], set.to_a) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/