Project

General

Profile

Feature #16833 ยป add-enumerable-empty.patch

f3ndot (Justin Bull), 05/06/2020 02:30 PM

View differences:

enum.c
}
}
/*
* call-seq:
* enum.empty? -> true or false
*
* Returns true if enumerable is empty?
*
* [].to_enum.empty? #=> true
* [nil].to_enum.empty? #=> false
* [false].to_enum.empty? #=> false
* [1, 2, 3].to_enum.empty? #=> false
*
*/
static VALUE
enum_empty(VALUE obj)
{
long len = RARRAY_LEN(enum_to_a(0, 0, enum_take(obj, INT2FIX(1))));
return len == 0 ? Qtrue : Qfalse;
}
/*
* call-seq:
......
rb_define_method(rb_mEnumerable, "group_by", enum_group_by, 0);
rb_define_method(rb_mEnumerable, "tally", enum_tally, 0);
rb_define_method(rb_mEnumerable, "first", enum_first, -1);
rb_define_method(rb_mEnumerable, "empty?", enum_empty, 0);
rb_define_method(rb_mEnumerable, "all?", enum_all, -1);
rb_define_method(rb_mEnumerable, "any?", enum_any, -1);
rb_define_method(rb_mEnumerable, "one?", enum_one, -1);
test/ruby/test_enum.rb
assert_equal(true, {a: 1, b: 2}.any?(->(kv) { kv == [:b, 2] }))
end
def test_empty
assert_equal(true, [].to_enum.empty?)
assert_equal(true, @empty.empty?)
assert_equal(false, @obj.empty?)
assert_equal(false, [nil].to_enum.empty?)
assert_equal(false, [0].to_enum.empty?)
assert_equal(false, [false].to_enum.empty?)
assert_equal(false, (1..Float::INFINITY).lazy.empty?)
end
def test_any_with_unused_block
assert_in_out_err [], <<-EOS, [], ["-:1: warning: given block not used"]
[1, 23].any?(1) {|x| x == 1 }
    (1-1/1)