mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	revert [Bug #9605]
* enum.c: revert r45284, r45286, r45292. it conflicts with existing behaviors. [Bug #9605] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45322 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									c28b5723f6
								
							
						
					
					
						commit
						3be926db2a
					
				
					 3 changed files with 18 additions and 135 deletions
				
			
		| 
						 | 
				
			
			@ -94,12 +94,6 @@ Sat Mar  8 13:30:39 2014  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		|||
	  expanding the buffer when ERANGE is returned.
 | 
			
		||||
	  [ruby-core:61325] [Bug #9600]
 | 
			
		||||
 | 
			
		||||
Sat Mar  8 07:35:40 2014  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		||||
 | 
			
		||||
	* enum.c (find_i): yield multiple values instead of a packed
 | 
			
		||||
	  array, so that lambda block can work.  with tests by Alex
 | 
			
		||||
	  Rothenberg.  [ruby-core:61340] [Bug #9605]
 | 
			
		||||
 | 
			
		||||
Fri Mar  7 19:29:13 2014  Eric Wong  <e@80x24.org>
 | 
			
		||||
 | 
			
		||||
	* vm_eval.c (vm_call0_body): use RARRAY_CONST_PTR
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										36
									
								
								enum.c
									
										
									
									
									
								
							
							
						
						
									
										36
									
								
								enum.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -62,7 +62,7 @@ grep_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
 | 
			
		|||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    if (RTEST(rb_funcall(memo->u1.value, id_eqq, 1, i))) {
 | 
			
		||||
	rb_ary_push(memo->u2.value, enum_yield(argc, argv));
 | 
			
		||||
	rb_ary_push(memo->u2.value, rb_yield(i));
 | 
			
		||||
    }
 | 
			
		||||
    return Qnil;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -180,7 +180,7 @@ find_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
 | 
			
		|||
{
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    if (RTEST(enum_yield(argc, argv))) {
 | 
			
		||||
    if (RTEST(rb_yield(i))) {
 | 
			
		||||
	NODE *memo = RNODE(memop);
 | 
			
		||||
	memo->u1.value = i;
 | 
			
		||||
	memo->u3.cnt = 1;
 | 
			
		||||
| 
						 | 
				
			
			@ -303,7 +303,7 @@ find_all_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
 | 
			
		|||
{
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    if (RTEST(enum_yield(argc, argv))) {
 | 
			
		||||
    if (RTEST(rb_yield(i))) {
 | 
			
		||||
	rb_ary_push(ary, i);
 | 
			
		||||
    }
 | 
			
		||||
    return Qnil;
 | 
			
		||||
| 
						 | 
				
			
			@ -355,7 +355,7 @@ reject_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
 | 
			
		|||
{
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    if (!RTEST(enum_yield(argc, argv))) {
 | 
			
		||||
    if (!RTEST(rb_yield(i))) {
 | 
			
		||||
	rb_ary_push(ary, i);
 | 
			
		||||
    }
 | 
			
		||||
    return Qnil;
 | 
			
		||||
| 
						 | 
				
			
			@ -674,7 +674,7 @@ partition_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arys))
 | 
			
		|||
    VALUE ary;
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    if (RTEST(enum_yield(argc, argv))) {
 | 
			
		||||
    if (RTEST(rb_yield(i))) {
 | 
			
		||||
	ary = memo->u1.value;
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
| 
						 | 
				
			
			@ -720,7 +720,7 @@ group_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
 | 
			
		|||
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    group = enum_yield(argc, argv);
 | 
			
		||||
    group = rb_yield(i);
 | 
			
		||||
    values = rb_hash_aref(hash, group);
 | 
			
		||||
    if (!RB_TYPE_P(values, T_ARRAY)) {
 | 
			
		||||
	values = rb_ary_new3(1, i);
 | 
			
		||||
| 
						 | 
				
			
			@ -846,7 +846,7 @@ sort_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _data))
 | 
			
		|||
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    v = enum_yield(argc, argv);
 | 
			
		||||
    v = rb_yield(i);
 | 
			
		||||
 | 
			
		||||
    if (RBASIC(ary)->klass) {
 | 
			
		||||
	rb_raise(rb_eRuntimeError, "sort_by reentered");
 | 
			
		||||
| 
						 | 
				
			
			@ -1249,7 +1249,7 @@ nmin_i(VALUE i, VALUE *_data, int argc, VALUE *argv)
 | 
			
		|||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    if (data->by)
 | 
			
		||||
	rb_ary_push(data->buf, enum_yield(argc, argv));
 | 
			
		||||
	rb_ary_push(data->buf, rb_yield(i));
 | 
			
		||||
    rb_ary_push(data->buf, i);
 | 
			
		||||
 | 
			
		||||
    data->curlen++;
 | 
			
		||||
| 
						 | 
				
			
			@ -1685,7 +1685,7 @@ min_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
 | 
			
		|||
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    v = enum_yield(argc, argv);
 | 
			
		||||
    v = rb_yield(i);
 | 
			
		||||
    if (memo->u1.value == Qundef) {
 | 
			
		||||
	memo->u1.value = v;
 | 
			
		||||
	memo->u2.value = i;
 | 
			
		||||
| 
						 | 
				
			
			@ -1745,7 +1745,7 @@ max_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
 | 
			
		|||
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    v = enum_yield(argc, argv);
 | 
			
		||||
    v = rb_yield(i);
 | 
			
		||||
    if (memo->u1.value == Qundef) {
 | 
			
		||||
	memo->u1.value = v;
 | 
			
		||||
	memo->u2.value = i;
 | 
			
		||||
| 
						 | 
				
			
			@ -1836,7 +1836,7 @@ minmax_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
 | 
			
		|||
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    vi = enum_yield(argc, argv);
 | 
			
		||||
    vi = rb_yield(i);
 | 
			
		||||
 | 
			
		||||
    if (memo->last_bv == Qundef) {
 | 
			
		||||
        memo->last_bv = vi;
 | 
			
		||||
| 
						 | 
				
			
			@ -2016,7 +2016,8 @@ enum_reverse_each(int argc, VALUE *argv, VALUE obj)
 | 
			
		|||
static VALUE
 | 
			
		||||
each_val_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p))
 | 
			
		||||
{
 | 
			
		||||
    enum_yield(argc, argv);
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
    rb_yield(i);
 | 
			
		||||
    return Qnil;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -2246,7 +2247,7 @@ zip_ary(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
 | 
			
		|||
	}
 | 
			
		||||
    }
 | 
			
		||||
    if (NIL_P(result)) {
 | 
			
		||||
	rb_yield_splat(tmp);
 | 
			
		||||
	rb_yield(tmp);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
	rb_ary_push(result, tmp);
 | 
			
		||||
| 
						 | 
				
			
			@ -2294,7 +2295,7 @@ zip_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
 | 
			
		|||
	}
 | 
			
		||||
    }
 | 
			
		||||
    if (NIL_P(result)) {
 | 
			
		||||
	rb_yield_splat(tmp);
 | 
			
		||||
	rb_yield(tmp);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
	rb_ary_push(result, tmp);
 | 
			
		||||
| 
						 | 
				
			
			@ -2488,7 +2489,7 @@ drop_while_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
 | 
			
		|||
    NODE *memo = RNODE(args);
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    if (!memo->u3.state && !RTEST(enum_yield(argc, argv))) {
 | 
			
		||||
    if (!memo->u3.state && !RTEST(rb_yield(i))) {
 | 
			
		||||
	memo->u3.state = TRUE;
 | 
			
		||||
    }
 | 
			
		||||
    if (memo->u3.state) {
 | 
			
		||||
| 
						 | 
				
			
			@ -2531,9 +2532,8 @@ cycle_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
 | 
			
		|||
{
 | 
			
		||||
    ENUM_WANT_SVALUE();
 | 
			
		||||
 | 
			
		||||
    if (argc == 1) i = rb_ary_new4(argc, argv);
 | 
			
		||||
    rb_ary_push(ary, i);
 | 
			
		||||
    enum_yield(argc, argv);
 | 
			
		||||
    rb_yield(i);
 | 
			
		||||
    return Qnil;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -2600,7 +2600,7 @@ enum_cycle(int argc, VALUE *argv, VALUE obj)
 | 
			
		|||
    if (len == 0) return Qnil;
 | 
			
		||||
    while (n < 0 || 0 < --n) {
 | 
			
		||||
        for (i=0; i<len; i++) {
 | 
			
		||||
            rb_yield_splat(RARRAY_AREF(ary, i));
 | 
			
		||||
            rb_yield(RARRAY_AREF(ary, i));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return Qnil;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -56,11 +56,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
    bug5801 = '[ruby-dev:45041]'
 | 
			
		||||
    @empty.grep(//)
 | 
			
		||||
    assert_nothing_raised(bug5801) {100.times {@empty.block.call}}
 | 
			
		||||
 | 
			
		||||
    a = []
 | 
			
		||||
    lambda = ->(x, i) {a << [x, i]}
 | 
			
		||||
    @obj.each_with_index.grep(proc{|x,i|x==2}, &lambda)
 | 
			
		||||
    assert_equal([[2, 1], [2, 4]], a)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_count
 | 
			
		||||
| 
						 | 
				
			
			@ -86,8 +81,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
    assert_equal(2, @obj.find {|x| x % 2 == 0 })
 | 
			
		||||
    assert_equal(nil, @obj.find {|x| false })
 | 
			
		||||
    assert_equal(:foo, @obj.find(proc { :foo }) {|x| false })
 | 
			
		||||
    cond = ->(x, i) { x % 2 == 0 }
 | 
			
		||||
    assert_equal([2, 1], @obj.each_with_index.find(&cond))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_find_index
 | 
			
		||||
| 
						 | 
				
			
			@ -100,14 +93,10 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
 | 
			
		||||
  def test_find_all
 | 
			
		||||
    assert_equal([1, 3, 1], @obj.find_all {|x| x % 2 == 1 })
 | 
			
		||||
    cond = ->(x, i) { x % 2 == 1 }
 | 
			
		||||
    assert_equal([[1, 0], [3, 2], [1, 3]], @obj.each_with_index.find_all(&cond))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_reject
 | 
			
		||||
    assert_equal([2, 3, 2], @obj.reject {|x| x < 2 })
 | 
			
		||||
    cond = ->(x, i) {x < 2}
 | 
			
		||||
    assert_equal([[2, 1], [3, 2], [2, 4]], @obj.each_with_index.reject(&cond))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_to_a
 | 
			
		||||
| 
						 | 
				
			
			@ -155,17 +144,11 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
 | 
			
		||||
  def test_partition
 | 
			
		||||
    assert_equal([[1, 3, 1], [2, 2]], @obj.partition {|x| x % 2 == 1 })
 | 
			
		||||
    cond = ->(x, i) { x % 2 == 1 }
 | 
			
		||||
    assert_equal([[[1, 0], [3, 2], [1, 3]], [[2, 1], [2, 4]]], @obj.each_with_index.partition(&cond))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_group_by
 | 
			
		||||
    h = { 1 => [1, 1], 2 => [2, 2], 3 => [3] }
 | 
			
		||||
    assert_equal(h, @obj.group_by {|x| x })
 | 
			
		||||
 | 
			
		||||
    h = {1=>[[1, 0], [1, 3]], 2=>[[2, 1], [2, 4]], 3=>[[3, 2]]}
 | 
			
		||||
    cond = ->(x, i) { x }
 | 
			
		||||
    assert_equal(h, @obj.each_with_index.group_by(&cond))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_first
 | 
			
		||||
| 
						 | 
				
			
			@ -181,9 +164,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
  def test_sort_by
 | 
			
		||||
    assert_equal([3, 2, 2, 1, 1], @obj.sort_by {|x| -x })
 | 
			
		||||
    assert_equal((1..300).to_a.reverse, (1..300).sort_by {|x| -x })
 | 
			
		||||
 | 
			
		||||
    cond = ->(x, i) { [-x, i] }
 | 
			
		||||
    assert_equal([[3, 2], [2, 1], [2, 4], [1, 0], [1, 3]], @obj.each_with_index.sort_by(&cond))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_all
 | 
			
		||||
| 
						 | 
				
			
			@ -225,8 +205,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
  def test_min
 | 
			
		||||
    assert_equal(1, @obj.min)
 | 
			
		||||
    assert_equal(3, @obj.min {|a,b| b <=> a })
 | 
			
		||||
    cond = ->((a, ia), (b, ib)) { (b <=> a).nonzero? or ia <=> ib }
 | 
			
		||||
    assert_equal([3, 2], @obj.each_with_index.min(&cond))
 | 
			
		||||
    ary = %w(albatross dog horse)
 | 
			
		||||
    assert_equal("albatross", ary.min)
 | 
			
		||||
    assert_equal("dog", ary.min {|a,b| a.length <=> b.length })
 | 
			
		||||
| 
						 | 
				
			
			@ -239,8 +217,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
  def test_max
 | 
			
		||||
    assert_equal(3, @obj.max)
 | 
			
		||||
    assert_equal(1, @obj.max {|a,b| b <=> a })
 | 
			
		||||
    cond = ->((a, ia), (b, ib)) { (b <=> a).nonzero? or ia <=> ib }
 | 
			
		||||
    assert_equal([1, 3], @obj.each_with_index.max(&cond))
 | 
			
		||||
    ary = %w(albatross dog horse)
 | 
			
		||||
    assert_equal("horse", ary.max)
 | 
			
		||||
    assert_equal("albatross", ary.max {|a,b| a.length <=> b.length })
 | 
			
		||||
| 
						 | 
				
			
			@ -263,8 +239,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
 | 
			
		||||
  def test_min_by
 | 
			
		||||
    assert_equal(3, @obj.min_by {|x| -x })
 | 
			
		||||
    cond = ->(x, i) { -x }
 | 
			
		||||
    assert_equal([3, 2], @obj.each_with_index.min_by(&cond))
 | 
			
		||||
    a = %w(albatross dog horse)
 | 
			
		||||
    assert_equal("dog", a.min_by {|x| x.length })
 | 
			
		||||
    assert_equal(3, [2,3,1].min_by {|x| -x })
 | 
			
		||||
| 
						 | 
				
			
			@ -273,8 +247,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
 | 
			
		||||
  def test_max_by
 | 
			
		||||
    assert_equal(1, @obj.max_by {|x| -x })
 | 
			
		||||
    cond = ->(x, i) { -x }
 | 
			
		||||
    assert_equal([1, 0], @obj.each_with_index.max_by(&cond))
 | 
			
		||||
    a = %w(albatross dog horse)
 | 
			
		||||
    assert_equal("albatross", a.max_by {|x| x.length })
 | 
			
		||||
    assert_equal(1, [2,3,1].max_by {|x| -x })
 | 
			
		||||
| 
						 | 
				
			
			@ -283,8 +255,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
 | 
			
		||||
  def test_minmax_by
 | 
			
		||||
    assert_equal([3, 1], @obj.minmax_by {|x| -x })
 | 
			
		||||
    cond = ->(x, i) { -x }
 | 
			
		||||
    assert_equal([[3, 2], [1, 0]], @obj.each_with_index.minmax_by(&cond))
 | 
			
		||||
    a = %w(albatross dog horse)
 | 
			
		||||
    assert_equal(["dog", "albatross"], a.minmax_by {|x| x.length })
 | 
			
		||||
    assert_equal([3, 1], [2,3,1].minmax_by {|x| -x })
 | 
			
		||||
| 
						 | 
				
			
			@ -332,10 +302,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
  def test_each_entry
 | 
			
		||||
    assert_equal([1, 2, 3], [1, 2, 3].each_entry.to_a)
 | 
			
		||||
    assert_equal([1, [1, 2]], Foo.new.each_entry.to_a)
 | 
			
		||||
    a = []
 | 
			
		||||
    cond = ->(x, i) { a << x }
 | 
			
		||||
    @obj.each_with_index.each_entry(&cond)
 | 
			
		||||
    assert_equal([1, 2, 3, 1, 2], a)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_zip
 | 
			
		||||
| 
						 | 
				
			
			@ -344,11 +310,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
    @obj.zip([:a, :b, :c]) {|x,y| a << [x, y] }
 | 
			
		||||
    assert_equal([[1,:a],[2,:b],[3,:c],[1,nil],[2,nil]], a)
 | 
			
		||||
 | 
			
		||||
    a = []
 | 
			
		||||
    cond = ->((x, i), y) { a << [x, y, i] }
 | 
			
		||||
    @obj.each_with_index.zip([:a, :b, :c], &cond)
 | 
			
		||||
    assert_equal([[1,:a,0],[2,:b,1],[3,:c,2],[1,nil,3],[2,nil,4]], a)
 | 
			
		||||
 | 
			
		||||
    a = []
 | 
			
		||||
    @obj.zip({a: "A", b: "B", c: "C"}) {|x,y| a << [x, y] }
 | 
			
		||||
    assert_equal([[1,[:a,"A"]],[2,[:b,"B"]],[3,[:c,"C"]],[1,nil],[2,nil]], a)
 | 
			
		||||
| 
						 | 
				
			
			@ -368,8 +329,6 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
 | 
			
		||||
  def test_take_while
 | 
			
		||||
    assert_equal([1,2], @obj.take_while {|x| x <= 2})
 | 
			
		||||
    cond = ->(x, i) {x <= 2}
 | 
			
		||||
    assert_equal([[1, 0], [2, 1]], @obj.each_with_index.take_while(&cond))
 | 
			
		||||
 | 
			
		||||
    bug5801 = '[ruby-dev:45040]'
 | 
			
		||||
    @empty.take_while {true}
 | 
			
		||||
| 
						 | 
				
			
			@ -382,19 +341,10 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
 | 
			
		||||
  def test_drop_while
 | 
			
		||||
    assert_equal([3,1,2], @obj.drop_while {|x| x <= 2})
 | 
			
		||||
    cond = ->(x, i) {x <= 2}
 | 
			
		||||
    assert_equal([[3, 2], [1, 3], [2, 4]], @obj.each_with_index.drop_while(&cond))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_cycle
 | 
			
		||||
    assert_equal([1,2,3,1,2,1,2,3,1,2], @obj.cycle.take(10))
 | 
			
		||||
    a = []
 | 
			
		||||
    @obj.cycle(2) {|x| a << x}
 | 
			
		||||
    assert_equal([1,2,3,1,2,1,2,3,1,2], a)
 | 
			
		||||
    a = []
 | 
			
		||||
    cond = ->(x, i) {a << x}
 | 
			
		||||
    @obj.each_with_index.cycle(2, &cond)
 | 
			
		||||
    assert_equal([1,2,3,1,2,1,2,3,1,2], a)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_callcc
 | 
			
		||||
| 
						 | 
				
			
			@ -509,65 +459,4 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
    assert_not_warn{ss.slice_before(/\A...\z/).to_a}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_detect
 | 
			
		||||
    @obj = ('a'..'z')
 | 
			
		||||
    assert_equal('c', @obj.detect {|x| x == 'c' })
 | 
			
		||||
 | 
			
		||||
    proc = Proc.new {|x| x == 'c' }
 | 
			
		||||
    assert_equal('c', @obj.detect(&proc))
 | 
			
		||||
 | 
			
		||||
    lambda = ->(x) { x == 'c' }
 | 
			
		||||
    assert_equal('c', @obj.detect(&lambda))
 | 
			
		||||
 | 
			
		||||
    assert_equal(['c',2], @obj.each_with_index.detect {|x, i| x == 'c' })
 | 
			
		||||
 | 
			
		||||
    proc2 = Proc.new {|x, i| x == 'c' }
 | 
			
		||||
    assert_equal(['c',2], @obj.each_with_index.detect(&proc2))
 | 
			
		||||
 | 
			
		||||
    bug9605 = '[ruby-core:61340]'
 | 
			
		||||
    lambda2 = ->(x, i) { x == 'c' }
 | 
			
		||||
    assert_equal(['c',2], @obj.each_with_index.detect(&lambda2))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_select
 | 
			
		||||
    @obj = ('a'..'z')
 | 
			
		||||
    assert_equal(['c'], @obj.select {|x| x == 'c' })
 | 
			
		||||
 | 
			
		||||
    proc = Proc.new {|x| x == 'c' }
 | 
			
		||||
    assert_equal(['c'], @obj.select(&proc))
 | 
			
		||||
 | 
			
		||||
    lambda = ->(x) { x == 'c' }
 | 
			
		||||
    assert_equal(['c'], @obj.select(&lambda))
 | 
			
		||||
 | 
			
		||||
    assert_equal([['c',2]], @obj.each_with_index.select {|x, i| x == 'c' })
 | 
			
		||||
 | 
			
		||||
    proc2 = Proc.new {|x, i| x == 'c' }
 | 
			
		||||
    assert_equal([['c',2]], @obj.each_with_index.select(&proc2))
 | 
			
		||||
 | 
			
		||||
    bug9605 = '[ruby-core:61340]'
 | 
			
		||||
    lambda2 = ->(x, i) { x == 'c' }
 | 
			
		||||
    assert_equal([['c',2]], @obj.each_with_index.select(&lambda2))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_map
 | 
			
		||||
    @obj = ('a'..'e')
 | 
			
		||||
    assert_equal(['A', 'B', 'C', 'D', 'E'], @obj.map {|x| x.upcase })
 | 
			
		||||
 | 
			
		||||
    proc = Proc.new {|x| x.upcase }
 | 
			
		||||
    assert_equal(['A', 'B', 'C', 'D', 'E'], @obj.map(&proc))
 | 
			
		||||
 | 
			
		||||
    lambda = ->(x) { x.upcase }
 | 
			
		||||
    assert_equal(['A', 'B', 'C', 'D', 'E'], @obj.map(&lambda))
 | 
			
		||||
 | 
			
		||||
    assert_equal([['A',0], ['B',1], ['C',2], ['D',3], ['E',4]],
 | 
			
		||||
      @obj.each_with_index.map {|x, i| [x.upcase, i] })
 | 
			
		||||
 | 
			
		||||
    proc2 = Proc.new {|x, i| [x.upcase, i] }
 | 
			
		||||
    assert_equal([['A',0], ['B',1], ['C',2], ['D',3], ['E',4]],
 | 
			
		||||
      @obj.each_with_index.map(&proc2))
 | 
			
		||||
 | 
			
		||||
    lambda2 = ->(x, i) { [x.upcase, i] }
 | 
			
		||||
    assert_equal([['A',0], ['B',1], ['C',2], ['D',3], ['E',4]],
 | 
			
		||||
      @obj.each_with_index.map(&lambda2))
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue