Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b585440

Browse files
authored
Merge pull request #6 from Shopify/merge-upstream
Merge upstream Ruby
2 parents 37a2ee8 + b709f55 commit b585440

File tree

284 files changed

+2396
-1232
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

284 files changed

+2396
-1232
lines changed

.github/probots.yml

-3
This file was deleted.

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Note that each entry is kept to a minimum, see links for details.
1919

2020
Outstanding ones only.
2121

22+
* Array
23+
24+
* Array#intersect? is added. [[Feature #15198]]
25+
2226
* Enumerable
2327

2428
* Enumerable#compact is added. [[Feature #17312]]
@@ -94,6 +98,7 @@ Excluding feature bug fixes.
9498

9599
[Feature #12194]: https://bugs.ruby-lang.org/issues/12194
96100
[Feature #14256]: https://bugs.ruby-lang.org/issues/14256
101+
[Feature #15198]: https://bugs.ruby-lang.org/issues/15198
97102
[Feature #16043]: https://bugs.ruby-lang.org/issues/16043
98103
[Feature #16806]: https://bugs.ruby-lang.org/issues/16806
99104
[Feature #17312]: https://bugs.ruby-lang.org/issues/17312

array.c

+58-2
Original file line numberDiff line numberDiff line change
@@ -4311,10 +4311,9 @@ static VALUE
43114311
take_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, cbarg))
43124312
{
43134313
VALUE *args = (VALUE *)cbarg;
4314-
if (args[1] == 0) rb_iter_break();
4315-
else args[1]--;
43164314
if (argc > 1) val = rb_ary_new4(argc, argv);
43174315
rb_ary_push(args[0], val);
4316+
if (--args[1] == 0) rb_iter_break();
43184317
return Qnil;
43194318
}
43204319

@@ -4324,6 +4323,7 @@ take_items(VALUE obj, long n)
43244323
VALUE result = rb_check_array_type(obj);
43254324
VALUE args[2];
43264325

4326+
if (n == 0) return result;
43274327
if (!NIL_P(result)) return rb_ary_subseq(result, 0, n);
43284328
result = rb_ary_new2(n);
43294329
args[0] = result; args[1] = (VALUE)n;
@@ -5567,6 +5567,61 @@ rb_ary_union_multi(int argc, VALUE *argv, VALUE ary)
55675567
return ary_union;
55685568
}
55695569

5570+
/*
5571+
* call-seq:
5572+
* ary.intersect?(other_ary) -> true or false
5573+
*
5574+
* Returns +true+ if the array and +other_ary+ have at least one element in
5575+
* common, otherwise returns +false+.
5576+
*
5577+
* a = [ 1, 2, 3 ]
5578+
* b = [ 3, 4, 5 ]
5579+
* c = [ 5, 6, 7 ]
5580+
* a.intersect?(b) #=> true
5581+
* a.intersect?(c) #=> false
5582+
*/
5583+
5584+
static VALUE
5585+
rb_ary_intersect_p(VALUE ary1, VALUE ary2)
5586+
{
5587+
VALUE hash, v, result, shorter, longer;
5588+
st_data_t vv;
5589+
long i;
5590+
5591+
ary2 = to_ary(ary2);
5592+
if (RARRAY_LEN(ary1) == 0 || RARRAY_LEN(ary2) == 0) return Qfalse;
5593+
5594+
if (RARRAY_LEN(ary1) <= SMALL_ARRAY_LEN && RARRAY_LEN(ary2) <= SMALL_ARRAY_LEN) {
5595+
for (i=0; i<RARRAY_LEN(ary1); i++) {
5596+
v = RARRAY_AREF(ary1, i);
5597+
if (rb_ary_includes_by_eql(ary2, v)) return Qtrue;
5598+
}
5599+
return Qfalse;
5600+
}
5601+
5602+
shorter = ary1;
5603+
longer = ary2;
5604+
if (RARRAY_LEN(ary1) > RARRAY_LEN(ary2)) {
5605+
longer = ary1;
5606+
shorter = ary2;
5607+
}
5608+
5609+
hash = ary_make_hash(shorter);
5610+
result = Qfalse;
5611+
5612+
for (i=0; i<RARRAY_LEN(longer); i++) {
5613+
v = RARRAY_AREF(longer, i);
5614+
vv = (st_data_t)v;
5615+
if (rb_hash_stlike_lookup(hash, vv, 0)) {
5616+
result = Qtrue;
5617+
break;
5618+
}
5619+
}
5620+
ary_recycle_hash(hash);
5621+
5622+
return result;
5623+
}
5624+
55705625
static VALUE
55715626
ary_max_generic(VALUE ary, long i, VALUE vmax)
55725627
{
@@ -8295,6 +8350,7 @@ Init_Array(void)
82958350
rb_define_method(rb_cArray, "union", rb_ary_union_multi, -1);
82968351
rb_define_method(rb_cArray, "difference", rb_ary_difference_multi, -1);
82978352
rb_define_method(rb_cArray, "intersection", rb_ary_intersection_multi, -1);
8353+
rb_define_method(rb_cArray, "intersect?", rb_ary_intersect_p, 1);
82988354
rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
82998355
rb_define_method(rb_cArray, "push", rb_ary_push_m, -1);
83008356
rb_define_alias(rb_cArray, "append", "push");

0 commit comments

Comments
 (0)