ruby-changes:52547
From: marcandre <ko1@a...>
Date: Sun, 16 Sep 2018 13:00:19 +0900 (JST)
Subject: [ruby-changes:52547] marcandRe: r64759 (trunk): Alias Struct#select as Struct#filter. Patch by Kenichi Kamiya.
marcandre 2018-09-16 13:00:14 +0900 (Sun, 16 Sep 2018) New Revision: 64759 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64759 Log: Alias Struct#select as Struct#filter. Patch by Kenichi Kamiya. [Fix GH-#1862] [#1784] Added files: trunk/spec/ruby/core/struct/filter_spec.rb trunk/spec/ruby/core/struct/shared/select.rb Modified files: trunk/NEWS trunk/spec/ruby/core/struct/select_spec.rb trunk/struct.c trunk/test/ruby/test_struct.rb Index: spec/ruby/core/struct/select_spec.rb =================================================================== --- spec/ruby/core/struct/select_spec.rb (revision 64758) +++ spec/ruby/core/struct/select_spec.rb (revision 64759) @@ -1,30 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/struct/select_spec.rb#L1 require_relative '../../spec_helper' -require_relative 'fixtures/classes' +require_relative 'shared/select' require_relative 'shared/accessor' require_relative '../enumerable/shared/enumeratorized' describe "Struct#select" do - it "raises an ArgumentError if given any non-block arguments" do - lambda { StructClasses::Car.new.select(1) { } }.should raise_error(ArgumentError) - end - - it "returns a new array of elements for which block is true" do - struct = StructClasses::Car.new("Toyota", "Tercel", "2000") - struct.select { |i| i == "2000" }.should == [ "2000" ] - end - - it "returns an instance of Array" do - struct = StructClasses::Car.new("Ford", "Escort", "1995") - struct.select { true }.should be_an_instance_of(Array) - end - - describe "without block" do - it "returns an instance of Enumerator" do - struct = Struct.new(:foo).new - struct.select.should be_an_instance_of(Enumerator) - end - end - + it_behaves_like :struct_select, :select it_behaves_like :struct_accessor, :select it_behaves_like :enumeratorized_with_origin_size, :select, Struct.new(:foo).new end Index: spec/ruby/core/struct/filter_spec.rb =================================================================== --- spec/ruby/core/struct/filter_spec.rb (nonexistent) +++ spec/ruby/core/struct/filter_spec.rb (revision 64759) @@ -0,0 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/struct/filter_spec.rb#L1 +require_relative '../../spec_helper' +require_relative 'shared/select' +require_relative 'shared/accessor' +require_relative '../enumerable/shared/enumeratorized' + +ruby_version_is "2.6" do + describe "Struct#filter" do + it_behaves_like :struct_select, :filter + it_behaves_like :struct_accessor, :filter + it_behaves_like :enumeratorized_with_origin_size, :filter, Struct.new(:foo).new + end +end Index: spec/ruby/core/struct/shared/select.rb =================================================================== --- spec/ruby/core/struct/shared/select.rb (nonexistent) +++ spec/ruby/core/struct/shared/select.rb (revision 64759) @@ -0,0 +1,25 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/struct/shared/select.rb#L1 +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' + +describe :struct_select, shared: true do + it "raises an ArgumentError if given any non-block arguments" do + lambda { StructClasses::Car.new.send(@method, 1) { } }.should raise_error(ArgumentError) + end + + it "returns a new array of elements for which block is true" do + struct = StructClasses::Car.new("Toyota", "Tercel", "2000") + struct.send(@method) { |i| i == "2000" }.should == [ "2000" ] + end + + it "returns an instance of Array" do + struct = StructClasses::Car.new("Ford", "Escort", "1995") + struct.send(@method) { true }.should be_an_instance_of(Array) + end + + describe "without block" do + it "returns an instance of Enumerator" do + struct = Struct.new(:foo).new + struct.send(@method).should be_an_instance_of(Enumerator) + end + end +end Index: struct.c =================================================================== --- struct.c (revision 64758) +++ struct.c (revision 64759) @@ -1297,6 +1297,7 @@ InitVM_Struct(void) https://github.com/ruby/ruby/blob/trunk/struct.c#L1297 rb_define_method(rb_cStruct, "[]", rb_struct_aref, 1); rb_define_method(rb_cStruct, "[]=", rb_struct_aset, 2); rb_define_method(rb_cStruct, "select", rb_struct_select, -1); + rb_define_method(rb_cStruct, "filter", rb_struct_select, -1); rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1); rb_define_method(rb_cStruct, "members", rb_struct_members_m, 0); Index: NEWS =================================================================== --- NEWS (revision 64758) +++ NEWS (revision 64759) @@ -195,6 +195,12 @@ sufficient information, see the ChangeLo https://github.com/ruby/ruby/blob/trunk/NEWS#L195 * `String#split` yields each substring to the block if given. [Feature #4780] +* `Struct` + + * Aliased method: + + * `Struct#filter` is a new alias for `Struct#select` [Feature #13784] + * `TracePoint` * New methods: Index: test/ruby/test_struct.rb =================================================================== --- test/ruby/test_struct.rb (revision 64758) +++ test/ruby/test_struct.rb (revision 64759) @@ -212,6 +212,13 @@ module TestStruct https://github.com/ruby/ruby/blob/trunk/test/ruby/test_struct.rb#L212 assert_raise(ArgumentError) { o.select(1) } end + def test_filter + klass = @Struct.new(:a, :b, :c, :d, :e, :f) + o = klass.new(1, 2, 3, 4, 5, 6) + assert_equal([1, 3, 5], o.filter {|v| v % 2 != 0 }) + assert_raise(ArgumentError) { o.filter(1) } + end + def test_big_struct klass1 = @Struct.new(*('a'..'z').map(&:to_sym)) o = klass1.new -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/