mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	Alias Struct#select as Struct#filter. Patch by Kenichi Kamiya.
[Fix GH-#1862] [#1784] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64759 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									2b3982ad1c
								
							
						
					
					
						commit
						c22b853737
					
				
					 6 changed files with 53 additions and 22 deletions
				
			
		
							
								
								
									
										6
									
								
								NEWS
									
										
									
									
									
								
							
							
						
						
									
										6
									
								
								NEWS
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -195,6 +195,12 @@ sufficient information, see the ChangeLog file or Redmine
 | 
			
		|||
        * `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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										12
									
								
								spec/ruby/core/struct/filter_spec.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								spec/ruby/core/struct/filter_spec.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,12 @@
 | 
			
		|||
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
 | 
			
		||||
| 
						 | 
				
			
			@ -1,30 +1,10 @@
 | 
			
		|||
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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										25
									
								
								spec/ruby/core/struct/shared/select.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								spec/ruby/core/struct/shared/select.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,25 @@
 | 
			
		|||
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
 | 
			
		||||
							
								
								
									
										1
									
								
								struct.c
									
										
									
									
									
								
							
							
						
						
									
										1
									
								
								struct.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1297,6 +1297,7 @@ InitVM_Struct(void)
 | 
			
		|||
    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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -212,6 +212,13 @@ module TestStruct
 | 
			
		|||
    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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue