summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
Diffstat (limited to 'array.c')
-rw-r--r--array.c3531
1 files changed, 1899 insertions, 1632 deletions
diff --git a/array.c b/array.c
index b4190702d2..b471823876 100644
--- a/array.c
+++ b/array.c
@@ -27,7 +27,10 @@
#include "probes.h"
#include "ruby/encoding.h"
#include "ruby/st.h"
+#include "ruby/thread.h"
#include "ruby/util.h"
+#include "ruby/ractor.h"
+#include "vm_core.h"
#include "builtin.h"
#if !ARRAY_DEBUG
@@ -37,15 +40,16 @@
#include "ruby_assert.h"
VALUE rb_cArray;
+VALUE rb_cArray_empty_frozen;
/* Flags of RArray
*
+ * 0: RARRAY_SHARED_FLAG (equal to ELTS_SHARED)
+ * The array is shared. The buffer this array points to is owned by
+ * another array (the shared root).
* 1: RARRAY_EMBED_FLAG
* The array is embedded (its contents follow the header, rather than
* being on a separately allocated buffer).
- * 2: RARRAY_SHARED_FLAG (equal to ELTS_SHARED)
- * The array is shared. The buffer this array points to is owned by
- * another array (the shared root).
* 3-9: RARRAY_EMBED_LEN
* The length of the array when RARRAY_EMBED_FLAG is set.
* 12: RARRAY_SHARED_ROOT_FLAG
@@ -76,47 +80,49 @@ should_be_T_ARRAY(VALUE ary)
return RB_TYPE_P(ary, T_ARRAY);
}
-#define ARY_HEAP_PTR(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
-#define ARY_HEAP_LEN(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
-#define ARY_HEAP_CAPA(a) (assert(!ARY_EMBED_P(a)), assert(!ARY_SHARED_ROOT_P(a)), \
+#define ARY_HEAP_PTR(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
+#define ARY_HEAP_LEN(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
+#define ARY_HEAP_CAPA(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(!ARY_SHARED_ROOT_P(a)), \
RARRAY(a)->as.heap.aux.capa)
-#define ARY_EMBED_PTR(a) (assert(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
+#define ARY_EMBED_PTR(a) (RUBY_ASSERT(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
#define ARY_EMBED_LEN(a) \
- (assert(ARY_EMBED_P(a)), \
+ (RUBY_ASSERT(ARY_EMBED_P(a)), \
(long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
(RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
-#define ARY_HEAP_SIZE(a) (assert(!ARY_EMBED_P(a)), assert(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE))
+#define ARY_HEAP_SIZE(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE))
-#define ARY_OWNS_HEAP_P(a) (assert(should_be_T_ARRAY((VALUE)(a))), \
+#define ARY_OWNS_HEAP_P(a) (RUBY_ASSERT(should_be_T_ARRAY((VALUE)(a))), \
!FL_TEST_RAW((a), RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG))
#define FL_SET_EMBED(a) do { \
- assert(!ARY_SHARED_P(a)); \
+ RUBY_ASSERT(!ARY_SHARED_P(a)); \
FL_SET((a), RARRAY_EMBED_FLAG); \
ary_verify(a); \
} while (0)
#define FL_UNSET_EMBED(ary) FL_UNSET((ary), RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK)
#define FL_SET_SHARED(ary) do { \
- assert(!ARY_EMBED_P(ary)); \
+ RUBY_ASSERT(!ARY_EMBED_P(ary)); \
FL_SET((ary), RARRAY_SHARED_FLAG); \
} while (0)
#define FL_UNSET_SHARED(ary) FL_UNSET((ary), RARRAY_SHARED_FLAG)
+#define ARY_SET_PTR_FORCE(ary, p) \
+ (RARRAY(ary)->as.heap.ptr = (p))
#define ARY_SET_PTR(ary, p) do { \
- assert(!ARY_EMBED_P(ary)); \
- assert(!OBJ_FROZEN(ary)); \
- RARRAY(ary)->as.heap.ptr = (p); \
+ RUBY_ASSERT(!ARY_EMBED_P(ary)); \
+ RUBY_ASSERT(!OBJ_FROZEN(ary)); \
+ ARY_SET_PTR_FORCE(ary, p); \
} while (0)
#define ARY_SET_EMBED_LEN(ary, n) do { \
long tmp_n = (n); \
- assert(ARY_EMBED_P(ary)); \
+ RUBY_ASSERT(ARY_EMBED_P(ary)); \
RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
} while (0)
#define ARY_SET_HEAP_LEN(ary, n) do { \
- assert(!ARY_EMBED_P(ary)); \
+ RUBY_ASSERT(!ARY_EMBED_P(ary)); \
RARRAY(ary)->as.heap.len = (n); \
} while (0)
#define ARY_SET_LEN(ary, n) do { \
@@ -126,15 +132,15 @@ should_be_T_ARRAY(VALUE ary)
else { \
ARY_SET_HEAP_LEN((ary), (n)); \
} \
- assert(RARRAY_LEN(ary) == (n)); \
+ RUBY_ASSERT(RARRAY_LEN(ary) == (n)); \
} while (0)
#define ARY_INCREASE_PTR(ary, n) do { \
- assert(!ARY_EMBED_P(ary)); \
- assert(!OBJ_FROZEN(ary)); \
+ RUBY_ASSERT(!ARY_EMBED_P(ary)); \
+ RUBY_ASSERT(!OBJ_FROZEN(ary)); \
RARRAY(ary)->as.heap.ptr += (n); \
} while (0)
#define ARY_INCREASE_LEN(ary, n) do { \
- assert(!OBJ_FROZEN(ary)); \
+ RUBY_ASSERT(!OBJ_FROZEN(ary)); \
if (ARY_EMBED_P(ary)) { \
ARY_SET_EMBED_LEN((ary), RARRAY_LEN(ary)+(n)); \
} \
@@ -145,31 +151,33 @@ should_be_T_ARRAY(VALUE ary)
#define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? ary_embed_capa(ary) : \
ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : ARY_HEAP_CAPA(ary))
+#define ARY_SET_CAPA_FORCE(ary, n) \
+ RARRAY(ary)->as.heap.aux.capa = (n);
#define ARY_SET_CAPA(ary, n) do { \
- assert(!ARY_EMBED_P(ary)); \
- assert(!ARY_SHARED_P(ary)); \
- assert(!OBJ_FROZEN(ary)); \
- RARRAY(ary)->as.heap.aux.capa = (n); \
+ RUBY_ASSERT(!ARY_EMBED_P(ary)); \
+ RUBY_ASSERT(!ARY_SHARED_P(ary)); \
+ RUBY_ASSERT(!OBJ_FROZEN(ary)); \
+ ARY_SET_CAPA_FORCE(ary, n); \
} while (0)
#define ARY_SHARED_ROOT_OCCUPIED(ary) (!OBJ_FROZEN(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1)
#define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \
- assert(ARY_SHARED_ROOT_P(ary)); \
- assert(!OBJ_FROZEN(ary)); \
- assert((value) >= 0); \
+ RUBY_ASSERT(ARY_SHARED_ROOT_P(ary)); \
+ RUBY_ASSERT(!OBJ_FROZEN(ary)); \
+ RUBY_ASSERT((value) >= 0); \
RARRAY(ary)->as.heap.aux.capa = (value); \
} while (0)
#define FL_SET_SHARED_ROOT(ary) do { \
- assert(!OBJ_FROZEN(ary)); \
- assert(!ARY_EMBED_P(ary)); \
+ RUBY_ASSERT(!OBJ_FROZEN(ary)); \
+ RUBY_ASSERT(!ARY_EMBED_P(ary)); \
FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \
} while (0)
static inline void
ARY_SET(VALUE a, long i, VALUE v)
{
- assert(!ARY_SHARED_P(a));
- assert(!OBJ_FROZEN(a));
+ RUBY_ASSERT(!ARY_SHARED_P(a));
+ RUBY_ASSERT(!OBJ_FROZEN(a));
RARRAY_ASET(a, i, v);
}
@@ -179,14 +187,16 @@ static long
ary_embed_capa(VALUE ary)
{
size_t size = rb_gc_obj_slot_size(ary) - offsetof(struct RArray, as.ary);
- assert(size % sizeof(VALUE) == 0);
+ RUBY_ASSERT(size % sizeof(VALUE) == 0);
return size / sizeof(VALUE);
}
static size_t
ary_embed_size(long capa)
{
- return offsetof(struct RArray, as.ary) + (sizeof(VALUE) * capa);
+ size_t size = offsetof(struct RArray, as.ary) + (sizeof(VALUE) * capa);
+ if (size < sizeof(struct RArray)) size = sizeof(struct RArray);
+ return size;
}
static bool
@@ -233,23 +243,22 @@ rb_ary_size_as_embedded(VALUE ary)
static VALUE
ary_verify_(VALUE ary, const char *file, int line)
{
- assert(RB_TYPE_P(ary, T_ARRAY));
+ RUBY_ASSERT(RB_TYPE_P(ary, T_ARRAY));
if (ARY_SHARED_P(ary)) {
VALUE root = ARY_SHARED_ROOT(ary);
const VALUE *ptr = ARY_HEAP_PTR(ary);
const VALUE *root_ptr = RARRAY_CONST_PTR(root);
long len = ARY_HEAP_LEN(ary), root_len = RARRAY_LEN(root);
- assert(ARY_SHARED_ROOT_P(root) || OBJ_FROZEN(root));
- assert(root_ptr <= ptr && ptr + len <= root_ptr + root_len);
+ RUBY_ASSERT(ARY_SHARED_ROOT_P(root) || OBJ_FROZEN(root));
+ RUBY_ASSERT(root_ptr <= ptr && ptr + len <= root_ptr + root_len);
ary_verify(root);
}
else if (ARY_EMBED_P(ary)) {
- assert(!ARY_SHARED_P(ary));
- assert(RARRAY_LEN(ary) <= ary_embed_capa(ary));
+ RUBY_ASSERT(!ARY_SHARED_P(ary));
+ RUBY_ASSERT(RARRAY_LEN(ary) <= ary_embed_capa(ary));
}
else {
-#if 1
const VALUE *ptr = RARRAY_CONST_PTR(ary);
long i, len = RARRAY_LEN(ary);
volatile VALUE v;
@@ -258,17 +267,10 @@ ary_verify_(VALUE ary, const char *file, int line)
v = ptr[i]; /* access check */
}
v = v;
-#endif
}
return ary;
}
-
-void
-rb_ary_verify(VALUE ary)
-{
- ary_verify(ary);
-}
#else
#define ary_verify(ary) ((void)0)
#endif
@@ -326,7 +328,7 @@ ary_memfill(VALUE ary, long beg, long size, VALUE val)
static void
ary_memcpy0(VALUE ary, long beg, long argc, const VALUE *argv, VALUE buff_owner_ary)
{
- assert(!ARY_SHARED_P(buff_owner_ary));
+ RUBY_ASSERT(!ARY_SHARED_P(buff_owner_ary));
if (argc > (int)(128/sizeof(VALUE)) /* is magic number (cache line size) */) {
rb_gc_writebarrier_remember(buff_owner_ary);
@@ -351,7 +353,7 @@ ary_memcpy(VALUE ary, long beg, long argc, const VALUE *argv)
}
static VALUE *
-ary_heap_alloc(size_t capa)
+ary_heap_alloc_buffer(size_t capa)
{
return ALLOC_N(VALUE, capa);
}
@@ -371,6 +373,7 @@ ary_heap_free(VALUE ary)
static size_t
ary_heap_realloc(VALUE ary, size_t new_capa)
{
+ RUBY_ASSERT(!OBJ_FROZEN(ary));
SIZED_REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, new_capa, ARY_HEAP_CAPA(ary));
ary_verify(ary);
@@ -380,7 +383,7 @@ ary_heap_realloc(VALUE ary, size_t new_capa)
void
rb_ary_make_embedded(VALUE ary)
{
- assert(rb_ary_embeddable_p(ary));
+ RUBY_ASSERT(rb_ary_embeddable_p(ary));
if (!ARY_EMBED_P(ary)) {
const VALUE *buf = ARY_HEAP_PTR(ary);
long len = ARY_HEAP_LEN(ary);
@@ -397,15 +400,15 @@ rb_ary_make_embedded(VALUE ary)
static void
ary_resize_capa(VALUE ary, long capacity)
{
- assert(RARRAY_LEN(ary) <= capacity);
- assert(!OBJ_FROZEN(ary));
- assert(!ARY_SHARED_P(ary));
+ RUBY_ASSERT(RARRAY_LEN(ary) <= capacity);
+ RUBY_ASSERT(!OBJ_FROZEN(ary));
+ RUBY_ASSERT(!ARY_SHARED_P(ary));
if (capacity > ary_embed_capa(ary)) {
size_t new_capa = capacity;
if (ARY_EMBED_P(ary)) {
long len = ARY_EMBED_LEN(ary);
- VALUE *ptr = ary_heap_alloc(capacity);
+ VALUE *ptr = ary_heap_alloc_buffer(capacity);
MEMCPY(ptr, ARY_EMBED_PTR(ary), VALUE, len);
FL_UNSET_EMBED(ary);
@@ -440,9 +443,12 @@ ary_shrink_capa(VALUE ary)
{
long capacity = ARY_HEAP_LEN(ary);
long old_capa = ARY_HEAP_CAPA(ary);
- assert(!ARY_SHARED_P(ary));
- assert(old_capa >= capacity);
- if (old_capa > capacity) ary_heap_realloc(ary, capacity);
+ RUBY_ASSERT(!ARY_SHARED_P(ary));
+ RUBY_ASSERT(old_capa >= capacity);
+ if (old_capa > capacity) {
+ size_t new_capa = ary_heap_realloc(ary, capacity);
+ ARY_SET_CAPA(ary, new_capa);
+ }
ary_verify(ary);
}
@@ -500,7 +506,7 @@ rb_ary_increment_share(VALUE shared_root)
{
if (!OBJ_FROZEN(shared_root)) {
long num = ARY_SHARED_ROOT_REFCNT(shared_root);
- assert(num >= 0);
+ RUBY_ASSERT(num >= 0);
ARY_SET_SHARED_ROOT_REFCNT(shared_root, num + 1);
}
return shared_root;
@@ -509,9 +515,9 @@ rb_ary_increment_share(VALUE shared_root)
static void
rb_ary_set_shared(VALUE ary, VALUE shared_root)
{
- assert(!ARY_EMBED_P(ary));
- assert(!OBJ_FROZEN(ary));
- assert(ARY_SHARED_ROOT_P(shared_root) || OBJ_FROZEN(shared_root));
+ RUBY_ASSERT(!ARY_EMBED_P(ary));
+ RUBY_ASSERT(!OBJ_FROZEN(ary));
+ RUBY_ASSERT(ARY_SHARED_ROOT_P(shared_root) || OBJ_FROZEN(shared_root));
rb_ary_increment_share(shared_root);
FL_SET_SHARED(ary);
@@ -523,6 +529,8 @@ rb_ary_set_shared(VALUE ary, VALUE shared_root)
static inline void
rb_ary_modify_check(VALUE ary)
{
+ RUBY_ASSERT(ruby_thread_has_gvl_p());
+
rb_check_frozen(ary);
ary_verify(ary);
}
@@ -556,11 +564,11 @@ rb_ary_cancel_sharing(VALUE ary)
rb_ary_decrement_share(shared_root);
}
else {
- VALUE *ptr = ary_heap_alloc(len);
+ VALUE *ptr = ary_heap_alloc_buffer(len);
MEMCPY(ptr, ARY_HEAP_PTR(ary), VALUE, len);
rb_ary_unshare(ary);
- ARY_SET_CAPA(ary, len);
- ARY_SET_PTR(ary, ptr);
+ ARY_SET_CAPA_FORCE(ary, len);
+ ARY_SET_PTR_FORCE(ary, ptr);
}
rb_gc_writebarrier_remember(ary);
@@ -625,21 +633,32 @@ ary_ensure_room_for_push(VALUE ary, long add_len)
/*
* call-seq:
- * array.freeze -> self
+ * freeze -> self
*
- * Freezes +self+; returns +self+:
+ * Freezes +self+ (if not already frozen); returns +self+:
*
* a = []
* a.frozen? # => false
* a.freeze
* a.frozen? # => true
*
- * An attempt to modify a frozen \Array raises FrozenError.
+ * No further changes may be made to +self+;
+ * raises FrozenError if a change is attempted.
+ *
+ * Related: Kernel#frozen?.
*/
VALUE
rb_ary_freeze(VALUE ary)
{
+ RUBY_ASSERT(RB_TYPE_P(ary, T_ARRAY));
+
+ if (OBJ_FROZEN(ary)) return ary;
+
+ if (!ARY_EMBED_P(ary) && !ARY_SHARED_P(ary) && !ARY_SHARED_ROOT_P(ary)) {
+ ary_shrink_capa(ary);
+ }
+
return rb_obj_freeze(ary);
}
@@ -666,7 +685,7 @@ static VALUE
ary_alloc_embed(VALUE klass, long capa)
{
size_t size = ary_embed_size(capa);
- assert(rb_gc_size_allocatable_p(size));
+ RUBY_ASSERT(rb_gc_size_allocatable_p(size));
NEWOBJ_OF(ary, struct RArray, klass,
T_ARRAY | RARRAY_EMBED_FLAG | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0),
size, 0);
@@ -683,6 +702,11 @@ ary_alloc_heap(VALUE klass)
NEWOBJ_OF(ary, struct RArray, klass,
T_ARRAY | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0),
sizeof(struct RArray), 0);
+
+ ary->as.heap.len = 0;
+ ary->as.heap.aux.capa = 0;
+ ary->as.heap.ptr = NULL;
+
return (VALUE)ary;
}
@@ -696,6 +720,8 @@ empty_ary_alloc(VALUE klass)
static VALUE
ary_new(VALUE klass, long capa)
{
+ RUBY_ASSERT(ruby_thread_has_gvl_p());
+
VALUE ary;
if (capa < 0) {
@@ -713,9 +739,9 @@ ary_new(VALUE klass, long capa)
else {
ary = ary_alloc_heap(klass);
ARY_SET_CAPA(ary, capa);
- assert(!ARY_EMBED_P(ary));
+ RUBY_ASSERT(!ARY_EMBED_P(ary));
- ARY_SET_PTR(ary, ary_heap_alloc(capa));
+ ARY_SET_PTR(ary, ary_heap_alloc_buffer(capa));
ARY_SET_HEAP_LEN(ary, 0);
}
@@ -777,7 +803,7 @@ static VALUE
ec_ary_alloc_embed(rb_execution_context_t *ec, VALUE klass, long capa)
{
size_t size = ary_embed_size(capa);
- assert(rb_gc_size_allocatable_p(size));
+ RUBY_ASSERT(rb_gc_size_allocatable_p(size));
NEWOBJ_OF(ary, struct RArray, klass,
T_ARRAY | RARRAY_EMBED_FLAG | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0),
size, ec);
@@ -794,6 +820,11 @@ ec_ary_alloc_heap(rb_execution_context_t *ec, VALUE klass)
NEWOBJ_OF(ary, struct RArray, klass,
T_ARRAY | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0),
sizeof(struct RArray), ec);
+
+ ary->as.heap.len = 0;
+ ary->as.heap.aux.capa = 0;
+ ary->as.heap.ptr = NULL;
+
return (VALUE)ary;
}
@@ -817,9 +848,9 @@ ec_ary_new(rb_execution_context_t *ec, VALUE klass, long capa)
else {
ary = ec_ary_alloc_heap(ec, klass);
ARY_SET_CAPA(ary, capa);
- assert(!ARY_EMBED_P(ary));
+ RUBY_ASSERT(!ARY_EMBED_P(ary));
- ARY_SET_PTR(ary, ary_heap_alloc(capa));
+ ARY_SET_PTR(ary, ary_heap_alloc_buffer(capa));
ARY_SET_HEAP_LEN(ary, 0);
}
@@ -881,7 +912,32 @@ rb_ary_free(VALUE ary)
}
}
-RUBY_FUNC_EXPORTED size_t
+static VALUE fake_ary_flags;
+
+static VALUE
+init_fake_ary_flags(void)
+{
+ struct RArray fake_ary = {0};
+ fake_ary.basic.flags = T_ARRAY;
+ VALUE ary = (VALUE)&fake_ary;
+ rb_ary_freeze(ary);
+ return fake_ary.basic.flags;
+}
+
+VALUE
+rb_setup_fake_ary(struct RArray *fake_ary, const VALUE *list, long len)
+{
+ fake_ary->basic.flags = fake_ary_flags;
+ RBASIC_CLEAR_CLASS((VALUE)fake_ary);
+
+ // bypass frozen checks
+ fake_ary->as.heap.ptr = list;
+ fake_ary->as.heap.len = len;
+ fake_ary->as.heap.aux.capa = len;
+ return (VALUE)fake_ary;
+}
+
+size_t
rb_ary_memsize(VALUE ary)
{
if (ARY_OWNS_HEAP_P(ary)) {
@@ -904,9 +960,6 @@ ary_make_shared(VALUE ary)
return ary;
}
else if (OBJ_FROZEN(ary)) {
- if (!ARY_EMBED_P(ary)) {
- ary_shrink_capa(ary);
- }
return ary;
}
else {
@@ -919,7 +972,7 @@ ary_make_shared(VALUE ary)
FL_SET_SHARED_ROOT(shared);
if (ARY_EMBED_P(ary)) {
- VALUE *ptr = ary_heap_alloc(capa);
+ VALUE *ptr = ary_heap_alloc_buffer(capa);
ARY_SET_PTR(shared, ptr);
ary_memcpy(shared, 0, len, RARRAY_CONST_PTR(ary));
@@ -949,7 +1002,7 @@ ary_make_substitution(VALUE ary)
if (ary_embeddable_p(len)) {
VALUE subst = rb_ary_new_capa(len);
- assert(ARY_EMBED_P(subst));
+ RUBY_ASSERT(ARY_EMBED_P(subst));
ary_memcpy(subst, 0, len, RARRAY_CONST_PTR(ary));
ARY_SET_EMBED_LEN(subst, len);
@@ -995,14 +1048,18 @@ rb_to_array(VALUE ary)
* call-seq:
* Array.try_convert(object) -> object, new_array, or nil
*
- * If +object+ is an \Array object, returns +object+.
+ * Attempts to return an array, based on the given +object+.
+ *
+ * If +object+ is an array, returns +object+.
*
- * Otherwise if +object+ responds to <tt>:to_ary</tt>,
- * calls <tt>object.to_ary</tt> and returns the result.
+ * Otherwise if +object+ responds to <tt>:to_ary</tt>.
+ * calls <tt>object.to_ary</tt>:
+ * if the return value is an array or +nil+, returns that value;
+ * if not, raises TypeError.
*
- * Returns +nil+ if +object+ does not respond to <tt>:to_ary</tt>
+ * Otherwise returns +nil+.
*
- * Raises an exception unless <tt>object.to_ary</tt> returns an \Array object.
+ * Related: see {Methods for Creating an Array}[rdoc-ref:Array@Methods+for+Creating+an+Array].
*/
static VALUE
@@ -1039,48 +1096,51 @@ rb_ary_s_new(int argc, VALUE *argv, VALUE klass)
* call-seq:
* Array.new -> new_empty_array
* Array.new(array) -> new_array
- * Array.new(size) -> new_array
- * Array.new(size, default_value) -> new_array
- * Array.new(size) {|index| ... } -> new_array
+ * Array.new(size, default_value = nil) -> new_array
+ * Array.new(size = 0) {|index| ... } -> new_array
*
- * Returns a new \Array.
+ * Returns a new array.
*
- * With no block and no arguments, returns a new empty \Array object.
+ * With no block and no argument given, returns a new empty array:
*
- * With no block and a single \Array argument +array+,
- * returns a new \Array formed from +array+:
+ * Array.new # => []
*
- * a = Array.new([:foo, 'bar', 2])
- * a.class # => Array
- * a # => [:foo, "bar", 2]
+ * With no block and array argument given, returns a new array with the same elements:
+ *
+ * Array.new([:foo, 'bar', 2]) # => [:foo, "bar", 2]
+ *
+ * With no block and integer argument given, returns a new array containing
+ * that many instances of the given +default_value+:
*
- * With no block and a single \Integer argument +size+,
- * returns a new \Array of the given size
- * whose elements are all +nil+:
+ * Array.new(0) # => []
+ * Array.new(3) # => [nil, nil, nil]
+ * Array.new(2, 3) # => [3, 3]
*
- * a = Array.new(3)
- * a # => [nil, nil, nil]
+ * With a block given, returns an array of the given +size+;
+ * calls the block with each +index+ in the range <tt>(0...size)</tt>;
+ * the element at that +index+ in the returned array is the blocks return value:
*
- * With no block and arguments +size+ and +default_value+,
- * returns an \Array of the given size;
- * each element is that same +default_value+:
+ * Array.new(3) {|index| "Element #{index}" } # => ["Element 0", "Element 1", "Element 2"]
*
- * a = Array.new(3, 'x')
- * a # => ['x', 'x', 'x']
+ * A common pitfall for new Rubyists is providing an expression as +default_value+:
*
- * With a block and argument +size+,
- * returns an \Array of the given size;
- * the block is called with each successive integer +index+;
- * the element for that +index+ is the return value from the block:
+ * array = Array.new(2, {})
+ * array # => [{}, {}]
+ * array[0][:a] = 1
+ * array # => [{a: 1}, {a: 1}], as array[0] and array[1] are same object
*
- * a = Array.new(3) {|index| "Element #{index}" }
- * a # => ["Element 0", "Element 1", "Element 2"]
+ * If you want the elements of the array to be distinct, you should pass a block:
*
- * Raises ArgumentError if +size+ is negative.
+ * array = Array.new(2) { {} }
+ * array # => [{}, {}]
+ * array[0][:a] = 1
+ * array # => [{a: 1}, {}], as array[0] and array[1] are different objects
*
- * With a block and no argument,
- * or a single argument +0+,
- * ignores the block and returns a new empty \Array.
+ * Raises TypeError if the first argument is not either an array
+ * or an {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]).
+ * Raises ArgumentError if the first argument is a negative integer.
+ *
+ * Related: see {Methods for Creating an Array}[rdoc-ref:Array@Methods+for+Creating+an+Array].
*/
static VALUE
@@ -1092,8 +1152,8 @@ rb_ary_initialize(int argc, VALUE *argv, VALUE ary)
rb_ary_modify(ary);
if (argc == 0) {
rb_ary_reset(ary);
- assert(ARY_EMBED_P(ary));
- assert(ARY_EMBED_LEN(ary) == 0);
+ RUBY_ASSERT(ARY_EMBED_P(ary));
+ RUBY_ASSERT(ARY_EMBED_LEN(ary) == 0);
if (rb_block_given_p()) {
rb_warning("given block not used");
}
@@ -1138,11 +1198,13 @@ rb_ary_initialize(int argc, VALUE *argv, VALUE ary)
}
/*
- * Returns a new array populated with the given objects.
+ * Returns a new array, populated with the given objects:
+ *
+ * Array[1, 'a', /^A/] # => [1, "a", /^A/]
+ * Array[] # => []
+ * Array.[](1, 'a', /^A/) # => [1, "a", /^A/]
*
- * Array.[]( 1, 'a', /^A/) # => [1, "a", /^A/]
- * Array[ 1, 'a', /^A/ ] # => [1, "a", /^A/]
- * [ 1, 'a', /^A/ ] # => [1, "a", /^A/]
+ * Related: see {Methods for Creating an Array}[rdoc-ref:Array@Methods+for+Creating+an+Array].
*/
static VALUE
@@ -1190,9 +1252,9 @@ rb_ary_store(VALUE ary, long idx, VALUE val)
static VALUE
ary_make_partial(VALUE ary, VALUE klass, long offset, long len)
{
- assert(offset >= 0);
- assert(len >= 0);
- assert(offset+len <= RARRAY_LEN(ary));
+ RUBY_ASSERT(offset >= 0);
+ RUBY_ASSERT(len >= 0);
+ RUBY_ASSERT(offset+len <= RARRAY_LEN(ary));
VALUE result = ary_alloc_heap(klass);
size_t embed_capa = ary_embed_capa(result);
@@ -1200,12 +1262,14 @@ ary_make_partial(VALUE ary, VALUE klass, long offset, long len)
FL_SET_EMBED(result);
ary_memcpy(result, 0, len, RARRAY_CONST_PTR(ary) + offset);
ARY_SET_EMBED_LEN(result, len);
- return result;
}
else {
VALUE shared = ary_make_shared(ary);
- assert(!ARY_EMBED_P(result));
+ /* The ary_make_shared call may allocate, which can trigger a GC
+ * compaction. This can cause the array to be embedded because it has
+ * a length of 0. */
+ FL_UNSET_EMBED(result);
ARY_SET_PTR(result, RARRAY_CONST_PTR(ary));
ARY_SET_LEN(result, RARRAY_LEN(ary));
@@ -1215,25 +1279,27 @@ ary_make_partial(VALUE ary, VALUE klass, long offset, long len)
ARY_SET_LEN(result, len);
ary_verify(shared);
- ary_verify(result);
- return result;
}
+
+ ary_verify(result);
+ return result;
}
static VALUE
ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step)
{
- assert(offset >= 0);
- assert(len >= 0);
- assert(offset+len <= RARRAY_LEN(ary));
- assert(step != 0);
+ RUBY_ASSERT(offset >= 0);
+ RUBY_ASSERT(len >= 0);
+ RUBY_ASSERT(offset+len <= RARRAY_LEN(ary));
+ RUBY_ASSERT(step != 0);
- const VALUE *values = RARRAY_CONST_PTR(ary);
const long orig_len = len;
if (step > 0 && step >= len) {
VALUE result = ary_new(klass, 1);
VALUE *ptr = (VALUE *)ARY_EMBED_PTR(result);
+ const VALUE *values = RARRAY_CONST_PTR(ary);
+
RB_OBJ_WRITE(result, ptr, values[offset]);
ARY_SET_EMBED_LEN(result, 1);
return result;
@@ -1251,6 +1317,8 @@ ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step)
VALUE result = ary_new(klass, len);
if (ARY_EMBED_P(result)) {
VALUE *ptr = (VALUE *)ARY_EMBED_PTR(result);
+ const VALUE *values = RARRAY_CONST_PTR(ary);
+
for (i = 0; i < len; ++i) {
RB_OBJ_WRITE(result, ptr+i, values[j]);
j += step;
@@ -1258,6 +1326,8 @@ ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step)
ARY_SET_EMBED_LEN(result, len);
}
else {
+ const VALUE *values = RARRAY_CONST_PTR(ary);
+
RARRAY_PTR_USE(result, ptr, {
for (i = 0; i < len; ++i) {
RB_OBJ_WRITE(result, ptr+i, values[j]);
@@ -1313,19 +1383,17 @@ ary_take_first_or_last(int argc, const VALUE *argv, VALUE ary, enum ary_take_pos
/*
* call-seq:
- * array << object -> self
+ * self << object -> self
*
- * Appends +object+ to +self+; returns +self+:
+ * Appends +object+ as the last element in +self+; returns +self+:
*
- * a = [:foo, 'bar', 2]
- * a << :baz # => [:foo, "bar", 2, :baz]
+ * [:foo, 'bar', 2] << :baz # => [:foo, "bar", 2, :baz]
*
- * Appends +object+ as one element, even if it is another \Array:
+ * Appends +object+ as a single element, even if it is another array:
*
- * a = [:foo, 'bar', 2]
- * a1 = a << [3, 4]
- * a1 # => [:foo, "bar", 2, [3, 4]]
+ * [:foo, 'bar', 2] << [3, 4] # => [:foo, "bar", 2, [3, 4]]
*
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
VALUE
@@ -1353,22 +1421,20 @@ rb_ary_cat(VALUE ary, const VALUE *argv, long len)
/*
* call-seq:
- * array.push(*objects) -> self
+ * push(*objects) -> self
+ * append(*objects) -> self
*
- * Appends trailing elements.
+ * Appends each argument in +objects+ to +self+; returns +self+:
*
- * Appends each argument in +objects+ to +self+; returns +self+:
+ * a = [:foo, 'bar', 2] # => [:foo, "bar", 2]
+ * a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat]
*
- * a = [:foo, 'bar', 2]
- * a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat]
+ * Appends each argument as a single element, even if it is another array:
*
- * Appends each argument as one element, even if it is another \Array:
+ * a = [:foo, 'bar', 2] # => [:foo, "bar", 2]
+ a.push([:baz, :bat], [:bam, :bad]) # => [:foo, "bar", 2, [:baz, :bat], [:bam, :bad]]
*
- * a = [:foo, 'bar', 2]
- * a1 = a.push([:baz, :bat], [:bam, :bad])
- * a1 # => [:foo, "bar", 2, [:baz, :bat], [:bam, :bad]]
- *
- * Related: #pop, #shift, #unshift.
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
static VALUE
@@ -1390,41 +1456,42 @@ rb_ary_pop(VALUE ary)
{
ary_resize_capa(ary, n * 2);
}
- --n;
- ARY_SET_LEN(ary, n);
+
+ VALUE obj = RARRAY_AREF(ary, n - 1);
+
+ ARY_SET_LEN(ary, n - 1);
ary_verify(ary);
- return RARRAY_AREF(ary, n);
+ return obj;
}
/*
* call-seq:
- * array.pop -> object or nil
- * array.pop(n) -> new_array
+ * pop -> object or nil
+ * pop(count) -> new_array
*
- * Removes and returns trailing elements.
+ * Removes and returns trailing elements of +self+.
*
- * When no argument is given and +self+ is not empty,
- * removes and returns the last element:
+ * With no argument given, removes and returns the last element, if available;
+ * otherwise returns +nil+:
*
* a = [:foo, 'bar', 2]
- * a.pop # => 2
- * a # => [:foo, "bar"]
- *
- * Returns +nil+ if the array is empty.
+ * a.pop # => 2
+ * a # => [:foo, "bar"]
+ * [].pop # => nil
*
- * When a non-negative \Integer argument +n+ is given and is in range,
+ * With non-negative integer argument +count+ given,
+ * returns a new array containing the trailing +count+ elements of +self+, as available:
*
- * removes and returns the last +n+ elements in a new \Array:
* a = [:foo, 'bar', 2]
* a.pop(2) # => ["bar", 2]
- *
- * If +n+ is positive and out of range,
- * removes and returns all elements:
+ * a # => [:foo]
*
* a = [:foo, 'bar', 2]
* a.pop(50) # => [:foo, "bar", 2]
+ * a # => []
*
- * Related: #push, #shift, #unshift.
+ * Related: Array#push;
+ * see also {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
@@ -1463,35 +1530,40 @@ rb_ary_shift(VALUE ary)
/*
* call-seq:
- * array.shift -> object or nil
- * array.shift(n) -> new_array
- *
- * Removes and returns leading elements.
+ * shift -> object or nil
+ * shift(count) -> new_array or nil
*
- * When no argument is given, removes and returns the first element:
+ * Removes and returns leading elements from +self+.
*
- * a = [:foo, 'bar', 2]
- * a.shift # => :foo
- * a # => ['bar', 2]
+ * With no argument, removes and returns one element, if available,
+ * or +nil+ otherwise:
*
- * Returns +nil+ if +self+ is empty.
+ * a = [0, 1, 2, 3]
+ * a.shift # => 0
+ * a # => [1, 2, 3]
+ * [].shift # => nil
*
- * When positive \Integer argument +n+ is given, removes the first +n+ elements;
- * returns those elements in a new \Array:
+ * With non-negative numeric argument +count+ given,
+ * removes and returns the first +count+ elements:
*
- * a = [:foo, 'bar', 2]
- * a.shift(2) # => [:foo, 'bar']
- * a # => [2]
- *
- * If +n+ is as large as or larger than <tt>self.length</tt>,
- * removes all elements; returns those elements in a new \Array:
+ * a = [0, 1, 2, 3]
+ * a.shift(2) # => [0, 1]
+ * a # => [2, 3]
+ * a.shift(1.1) # => [2]
+ * a # => [3]
+ * a.shift(0) # => []
+ * a # => [3]
+ *
+ * If +count+ is large,
+ * removes and returns all elements:
*
- * a = [:foo, 'bar', 2]
- * a.shift(3) # => [:foo, 'bar', 2]
+ * a = [0, 1, 2, 3]
+ * a.shift(50) # => [0, 1, 2, 3]
+ * a # => []
*
- * If +n+ is zero, returns a new empty \Array; +self+ is unmodified.
+ * If +self+ is empty, returns a new empty array.
*
- * Related: #push, #pop, #unshift.
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
@@ -1556,7 +1628,7 @@ make_room_for_unshift(VALUE ary, const VALUE *head, VALUE *sharedp, int argc, lo
head = sharedp + argc + room;
}
ARY_SET_PTR(ary, head - argc);
- assert(ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(ary)));
+ RUBY_ASSERT(ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(ary)));
ary_verify(ary);
return ARY_SHARED_ROOT(ary);
@@ -1632,14 +1704,16 @@ ary_ensure_room_for_unshift(VALUE ary, int argc)
/*
* call-seq:
- * array.unshift(*objects) -> self
+ * unshift(*objects) -> self
+ * prepend(*objects) -> self
*
* Prepends the given +objects+ to +self+:
*
* a = [:foo, 'bar', 2]
* a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
*
- * Related: #push, #pop, #shift.
+ * Related: Array#shift;
+ * see also {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
VALUE
@@ -1715,25 +1789,38 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e);
/*
* call-seq:
- * array[index] -> object or nil
- * array[start, length] -> object or nil
- * array[range] -> object or nil
- * array[aseq] -> object or nil
- * array.slice(index) -> object or nil
- * array.slice(start, length) -> object or nil
- * array.slice(range) -> object or nil
- * array.slice(aseq) -> object or nil
+ * self[offset] -> object or nil
+ * self[offset, size] -> object or nil
+ * self[range] -> object or nil
+ * self[aseq] -> object or nil
*
* Returns elements from +self+; does not modify +self+.
*
- * When a single \Integer argument +index+ is given, returns the element at offset +index+:
+ * In brief:
+ *
+ * a = [:foo, 'bar', 2]
+ *
+ * # Single argument offset: returns one element.
+ * a[0] # => :foo # Zero-based index.
+ * a[-1] # => 2 # Negative index counts backwards from end.
+ *
+ * # Arguments offset and size: returns an array.
+ * a[1, 2] # => ["bar", 2]
+ * a[-2, 2] # => ["bar", 2] # Negative offset counts backwards from end.
+ *
+ * # Single argument range: returns an array.
+ * a[0..1] # => [:foo, "bar"]
+ * a[0..-2] # => [:foo, "bar"] # Negative range-begin counts backwards from end.
+ * a[-2..2] # => ["bar", 2] # Negative range-end counts backwards from end.
+ *
+ * When a single integer argument +offset+ is given, returns the element at offset +offset+:
*
* a = [:foo, 'bar', 2]
* a[0] # => :foo
* a[2] # => 2
* a # => [:foo, "bar", 2]
*
- * If +index+ is negative, counts relative to the end of +self+:
+ * If +offset+ is negative, counts backwards from the end of +self+:
*
* a = [:foo, 'bar', 2]
* a[-1] # => 2
@@ -1741,35 +1828,35 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e);
*
* If +index+ is out of range, returns +nil+.
*
- * When two \Integer arguments +start+ and +length+ are given,
- * returns a new \Array of size +length+ containing successive elements beginning at offset +start+:
+ * When two Integer arguments +offset+ and +size+ are given,
+ * returns a new array of size +size+ containing successive elements beginning at offset +offset+:
*
* a = [:foo, 'bar', 2]
* a[0, 2] # => [:foo, "bar"]
* a[1, 2] # => ["bar", 2]
*
- * If <tt>start + length</tt> is greater than <tt>self.length</tt>,
- * returns all elements from offset +start+ to the end:
+ * If <tt>offset + size</tt> is greater than <tt>self.size</tt>,
+ * returns all elements from offset +offset+ to the end:
*
* a = [:foo, 'bar', 2]
* a[0, 4] # => [:foo, "bar", 2]
* a[1, 3] # => ["bar", 2]
* a[2, 2] # => [2]
*
- * If <tt>start == self.size</tt> and <tt>length >= 0</tt>,
- * returns a new empty \Array.
+ * If <tt>offset == self.size</tt> and <tt>size >= 0</tt>,
+ * returns a new empty array.
*
- * If +length+ is negative, returns +nil+.
+ * If +size+ is negative, returns +nil+.
*
- * When a single \Range argument +range+ is given,
- * treats <tt>range.min</tt> as +start+ above
- * and <tt>range.size</tt> as +length+ above:
+ * When a single Range argument +range+ is given,
+ * treats <tt>range.min</tt> as +offset+ above
+ * and <tt>range.size</tt> as +size+ above:
*
* a = [:foo, 'bar', 2]
* a[0..1] # => [:foo, "bar"]
* a[1..2] # => ["bar", 2]
*
- * Special case: If <tt>range.start == a.size</tt>, returns a new empty \Array.
+ * Special case: If <tt>range.start == a.size</tt>, returns a new empty array.
*
* If <tt>range.end</tt> is negative, calculates the end index from the end:
*
@@ -1793,7 +1880,7 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e);
* a[4..-1] # => nil
*
* When a single Enumerator::ArithmeticSequence argument +aseq+ is given,
- * returns an \Array of elements corresponding to the indexes produced by
+ * returns an array of elements corresponding to the indexes produced by
* the sequence.
*
* a = ['--', 'data1', '--', 'data2', '--', 'data3']
@@ -1815,6 +1902,7 @@ static VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e);
* # Raises TypeError (no implicit conversion of Symbol into Integer):
* a[:foo]
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
VALUE
@@ -1862,13 +1950,26 @@ rb_ary_aref1(VALUE ary, VALUE arg)
/*
* call-seq:
- * array.at(index) -> object
+ * at(index) -> object or nil
+ *
+ * Returns the element of +self+ specified by the given +index+
+ * or +nil+ if there is no such element;
+ * +index+ must be an
+ * {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects].
+ *
+ * For non-negative +index+, returns the element of +self+ at offset +index+:
*
- * Returns the element at \Integer offset +index+; does not modify +self+.
* a = [:foo, 'bar', 2]
- * a.at(0) # => :foo
- * a.at(2) # => 2
+ * a.at(0) # => :foo
+ * a.at(2) # => 2
+ * a.at(2.0) # => 2
+ *
+ * For negative +index+, counts backwards from the end of +self+:
+ *
+ * a.at(-2) # => "bar"
*
+ * Related: Array#[];
+ * see also {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
VALUE
@@ -1917,17 +2018,19 @@ rb_ary_last(int argc, const VALUE *argv, VALUE ary) // used by parse.y
/*
* call-seq:
- * array.fetch(index) -> element
- * array.fetch(index, default_value) -> element
- * array.fetch(index) {|index| ... } -> element
+ * fetch(index) -> element
+ * fetch(index, default_value) -> element or default_value
+ * fetch(index) {|index| ... } -> element or block_return_value
*
- * Returns the element at offset +index+.
+ * Returns the element of +self+ at offset +index+ if +index+ is in range; +index+ must be an
+ * {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects].
*
- * With the single \Integer argument +index+,
+ * With the single argument +index+ and no block,
* returns the element at offset +index+:
*
* a = [:foo, 'bar', 2]
- * a.fetch(1) # => "bar"
+ * a.fetch(1) # => "bar"
+ * a.fetch(1.1) # => "bar"
*
* If +index+ is negative, counts from the end of the array:
*
@@ -1935,12 +2038,12 @@ rb_ary_last(int argc, const VALUE *argv, VALUE ary) // used by parse.y
* a.fetch(-1) # => 2
* a.fetch(-2) # => "bar"
*
- * With arguments +index+ and +default_value+,
- * returns the element at offset +index+ if index is in range,
- * otherwise returns +default_value+:
+ * With arguments +index+ and +default_value+ (which may be any object) and no block,
+ * returns +default_value+ if +index+ is out-of-range:
*
* a = [:foo, 'bar', 2]
- * a.fetch(1, nil) # => "bar"
+ * a.fetch(1, nil) # => "bar"
+ * a.fetch(3, :foo) # => :foo
*
* With argument +index+ and a block,
* returns the element at offset +index+ if index is in range
@@ -1950,6 +2053,7 @@ rb_ary_last(int argc, const VALUE *argv, VALUE ary) // used by parse.y
* a.fetch(1) {|index| raise 'Cannot happen' } # => "bar"
* a.fetch(50) {|index| "Value for #{index}" } # => "Value for 50"
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -1981,14 +2085,106 @@ rb_ary_fetch(int argc, VALUE *argv, VALUE ary)
}
/*
+ * call-seq:
+ * find(if_none_proc = nil) {|element| ... } -> object or nil
+ * find(if_none_proc = nil) -> enumerator
+ *
+ * Returns the first element for which the block returns a truthy value.
+ *
+ * With a block given, calls the block with successive elements of the array;
+ * returns the first element for which the block returns a truthy value:
+ *
+ * [1, 3, 5].find {|element| element > 2} # => 3
+ *
+ * If no such element is found, calls +if_none_proc+ and returns its return value.
+ *
+ * [1, 3, 5].find(proc {-1}) {|element| element > 12} # => -1
+ *
+ * With no block given, returns an Enumerator.
+ *
+ */
+
+static VALUE
+rb_ary_find(int argc, VALUE *argv, VALUE ary)
+{
+ VALUE if_none;
+ long idx;
+
+ RETURN_ENUMERATOR(ary, argc, argv);
+ if_none = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
+
+ for (idx = 0; idx < RARRAY_LEN(ary); idx++) {
+ VALUE elem = RARRAY_AREF(ary, idx);
+ if (RTEST(rb_yield(elem))) {
+ return elem;
+ }
+ }
+
+ if (!NIL_P(if_none)) {
+ return rb_funcallv(if_none, idCall, 0, 0);
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * rfind(if_none_proc = nil) {|element| ... } -> object or nil
+ * rfind(if_none_proc = nil) -> enumerator
+ *
+ * Returns the last element for which the block returns a truthy value.
+ *
+ * With a block given, calls the block with successive elements of the array in
+ * reverse order; returns the first element for which the block returns a truthy
+ * value:
+ *
+ * [1, 2, 3, 4, 5, 6].rfind {|element| element < 5} # => 4
+ *
+ * If no such element is found, calls +if_none_proc+ and returns its return value.
+ *
+ * [1, 2, 3, 4].rfind(proc {0}) {|element| element < -2} # => 0
+ *
+ * With no block given, returns an Enumerator.
+ *
+ */
+
+static VALUE
+rb_ary_rfind(int argc, VALUE *argv, VALUE ary)
+{
+ VALUE if_none;
+ long len, idx;
+
+ RETURN_ENUMERATOR(ary, argc, argv);
+ if_none = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
+
+ idx = RARRAY_LEN(ary);
+ while (idx--) {
+ VALUE elem = RARRAY_AREF(ary, idx);
+ if (RTEST(rb_yield(elem))) {
+ return elem;
+ }
+
+ len = RARRAY_LEN(ary);
+ idx = (idx >= len) ? len : idx;
+ }
+
+ if (!NIL_P(if_none)) {
+ return rb_funcallv(if_none, idCall, 0, 0);
+ }
+ return Qnil;
+}
+
+/*
* call-seq:
- * array.index(object) -> integer or nil
- * array.index {|element| ... } -> integer or nil
- * array.index -> new_enumerator
+ * find_index(object) -> integer or nil
+ * find_index {|element| ... } -> integer or nil
+ * find_index -> new_enumerator
+ * index(object) -> integer or nil
+ * index {|element| ... } -> integer or nil
+ * index -> new_enumerator
*
- * Returns the index of a specified element.
+ * Returns the zero-based integer index of a specified element, or +nil+.
*
- * When argument +object+ is given but no block,
+ * With only argument +object+ given,
* returns the index of the first element +element+
* for which <tt>object == element</tt>:
*
@@ -1997,7 +2193,7 @@ rb_ary_fetch(int argc, VALUE *argv, VALUE ary)
*
* Returns +nil+ if no such element found.
*
- * When both argument +object+ and a block are given,
+ * With only a block given,
* calls the block with each successive element;
* returns the index of the first element for which the block returns a truthy value:
*
@@ -2006,14 +2202,9 @@ rb_ary_fetch(int argc, VALUE *argv, VALUE ary)
*
* Returns +nil+ if the block never returns a truthy value.
*
- * When neither an argument nor a block is given, returns a new Enumerator:
+ * With neither an argument nor a block given, returns a new Enumerator.
*
- * a = [:foo, 'bar', 2]
- * e = a.index
- * e # => #<Enumerator: [:foo, "bar", 2]:index>
- * e.each {|element| element == 'bar' } # => 1
- *
- * Related: #rindex.
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -2046,20 +2237,20 @@ rb_ary_index(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.rindex(object) -> integer or nil
- * array.rindex {|element| ... } -> integer or nil
- * array.rindex -> new_enumerator
+ * rindex(object) -> integer or nil
+ * rindex {|element| ... } -> integer or nil
+ * rindex -> new_enumerator
*
* Returns the index of the last element for which <tt>object == element</tt>.
*
- * When argument +object+ is given but no block, returns the index of the last such element found:
+ * With argument +object+ given, returns the index of the last such element found:
*
* a = [:foo, 'bar', 2, 'bar']
* a.rindex('bar') # => 3
*
* Returns +nil+ if no such object found.
*
- * When a block is given but no argument, calls the block with each successive element;
+ * With a block given, calls the block with each successive element;
* returns the index of the last element for which the block returns a truthy value:
*
* a = [:foo, 'bar', 2, 'bar']
@@ -2067,14 +2258,9 @@ rb_ary_index(int argc, VALUE *argv, VALUE ary)
*
* Returns +nil+ if the block never returns a truthy value.
*
- * When neither an argument nor a block is given, returns a new \Enumerator:
- *
- * a = [:foo, 'bar', 2, 'bar']
- * e = a.rindex
- * e # => #<Enumerator: [:foo, "bar", 2, "bar"]:rindex>
- * e.each {|element| element == 'bar' } # => 3
+ * When neither an argument nor a block is given, returns a new Enumerator.
*
- * Related: #index.
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -2176,9 +2362,14 @@ rb_ary_splice(VALUE ary, long beg, long len, const VALUE *rptr, long rlen)
ARY_SET_LEN(ary, alen);
}
if (rlen > 0) {
- if (rofs != -1) rptr = RARRAY_CONST_PTR(ary) + rofs;
- /* give up wb-protected ary */
- RB_OBJ_WB_UNPROTECT_FOR(ARRAY, ary);
+ if (rofs == -1) {
+ rb_gc_writebarrier_remember(ary);
+ }
+ else {
+ /* In this case, we're copying from a region in this array, so
+ * we don't need to fire the write barrier. */
+ rptr = RARRAY_CONST_PTR(ary) + rofs;
+ }
/* do not use RARRAY_PTR() because it can causes GC.
* ary can contain T_NONE object because it is not cleared.
@@ -2216,7 +2407,7 @@ rb_ary_resize(VALUE ary, long len)
rb_raise(rb_eIndexError, "index %ld too big", len);
}
if (len > olen) {
- if (len >= ARY_CAPA(ary)) {
+ if (len > ARY_CAPA(ary)) {
ary_double_capa(ary, len);
}
ary_mem_clear(ary, olen, len - olen);
@@ -2266,13 +2457,41 @@ ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val)
/*
* call-seq:
- * array[index] = object -> object
- * array[start, length] = object -> object
- * array[range] = object -> object
- *
- * Assigns elements in +self+; returns the given +object+.
- *
- * When \Integer argument +index+ is given, assigns +object+ to an element in +self+.
+ * self[index] = object -> object
+ * self[start, length] = object -> object
+ * self[range] = object -> object
+ *
+ * Assigns elements in +self+, based on the given +object+; returns +object+.
+ *
+ * In brief:
+ *
+ * a_orig = [:foo, 'bar', 2]
+ *
+ * # With argument index.
+ * a = a_orig.dup
+ * a[0] = 'foo' # => "foo"
+ * a # => ["foo", "bar", 2]
+ * a = a_orig.dup
+ * a[7] = 'foo' # => "foo"
+ * a # => [:foo, "bar", 2, nil, nil, nil, nil, "foo"]
+ *
+ * # With arguments start and length.
+ * a = a_orig.dup
+ * a[0, 2] = 'foo' # => "foo"
+ * a # => ["foo", 2]
+ * a = a_orig.dup
+ * a[6, 50] = 'foo' # => "foo"
+ * a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
+ *
+ * # With argument range.
+ * a = a_orig.dup
+ * a[0..1] = 'foo' # => "foo"
+ * a # => ["foo", 2]
+ * a = a_orig.dup
+ * a[6..50] = 'foo' # => "foo"
+ * a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
+ *
+ * When Integer argument +index+ is given, assigns +object+ to an element in +self+.
*
* If +index+ is non-negative, assigns +object+ the element at offset +index+:
*
@@ -2292,7 +2511,7 @@ ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val)
* a[-1] = 'two' # => "two"
* a # => [:foo, "bar", "two"]
*
- * When \Integer arguments +start+ and +length+ are given and +object+ is not an \Array,
+ * When Integer arguments +start+ and +length+ are given and +object+ is not an array,
* removes <tt>length - 1</tt> elements beginning at offset +start+,
* and assigns +object+ at offset +start+:
*
@@ -2327,7 +2546,7 @@ ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val)
* a[1, 5] = 'foo' # => "foo"
* a # => [:foo, "foo"]
*
- * When \Range argument +range+ is given and +object+ is an \Array,
+ * When Range argument +range+ is given and +object+ is not an array,
* removes <tt>length - 1</tt> elements beginning at offset +start+,
* and assigns +object+ at offset +start+:
*
@@ -2342,7 +2561,8 @@ ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val)
* a # => [:foo, "foo"]
*
* If the array length is less than <tt>range.begin</tt>,
- * assigns +object+ at offset <tt>range.begin</tt>, and ignores +length+:
+ * extends the array with +nil+, assigns +object+ at offset <tt>range.begin</tt>,
+ * and ignores +length+:
*
* a = [:foo, 'bar', 2]
* a[6..50] = 'foo' # => "foo"
@@ -2376,6 +2596,7 @@ ary_aset_by_rb_ary_splice(VALUE ary, long beg, long len, VALUE val)
* a[1..5] = 'foo' # => "foo"
* a # => [:foo, "foo"]
*
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
static VALUE
@@ -2405,38 +2626,38 @@ rb_ary_aset(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.insert(index, *objects) -> self
+ * insert(index, *objects) -> self
*
- * Inserts given +objects+ before or after the element at \Integer index +offset+;
+ * Inserts the given +objects+ as elements of +self+;
* returns +self+.
*
- * When +index+ is non-negative, inserts all given +objects+
- * before the element at offset +index+:
+ * When +index+ is non-negative, inserts +objects+
+ * _before_ the element at offset +index+:
*
- * a = [:foo, 'bar', 2]
- * a.insert(1, :bat, :bam) # => [:foo, :bat, :bam, "bar", 2]
+ * a = ['a', 'b', 'c'] # => ["a", "b", "c"]
+ * a.insert(1, :x, :y, :z) # => ["a", :x, :y, :z, "b", "c"]
*
* Extends the array if +index+ is beyond the array (<tt>index >= self.size</tt>):
*
- * a = [:foo, 'bar', 2]
- * a.insert(5, :bat, :bam)
- * a # => [:foo, "bar", 2, nil, nil, :bat, :bam]
+ * a = ['a', 'b', 'c'] # => ["a", "b", "c"]
+ * a.insert(5, :x, :y, :z) # => ["a", "b", "c", nil, nil, :x, :y, :z]
*
- * Does nothing if no objects given:
+ * When +index+ is negative, inserts +objects+
+ * _after_ the element at offset <tt>index + self.size</tt>:
*
- * a = [:foo, 'bar', 2]
- * a.insert(1)
- * a.insert(50)
- * a.insert(-50)
- * a # => [:foo, "bar", 2]
+ * a = ['a', 'b', 'c'] # => ["a", "b", "c"]
+ * a.insert(-2, :x, :y, :z) # => ["a", "b", :x, :y, :z, "c"]
*
- * When +index+ is negative, inserts all given +objects+
- * _after_ the element at offset <tt>index+self.size</tt>:
+ * With no +objects+ given, does nothing:
*
- * a = [:foo, 'bar', 2]
- * a.insert(-2, :bat, :bam)
- * a # => [:foo, "bar", :bat, :bam, 2]
+ * a = ['a', 'b', 'c'] # => ["a", "b", "c"]
+ * a.insert(1) # => ["a", "b", "c"]
+ * a.insert(50) # => ["a", "b", "c"]
+ * a.insert(-50) # => ["a", "b", "c"]
+ *
+ * Raises IndexError if +objects+ are given and +index+ is negative and out of range.
*
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
static VALUE
@@ -2472,14 +2693,27 @@ ary_enum_length(VALUE ary, VALUE args, VALUE eobj)
return rb_ary_length(ary);
}
+// Primitive to avoid a race condition in Array#each.
+// Return `true` and write `value` and `index` if the element exists.
+static VALUE
+ary_fetch_next(VALUE self, VALUE *index, VALUE *value)
+{
+ long i = NUM2LONG(*index);
+ if (i >= RARRAY_LEN(self)) {
+ return Qfalse;
+ }
+ *value = RARRAY_AREF(self, i);
+ *index = LONG2NUM(i + 1);
+ return Qtrue;
+}
+
/*
* call-seq:
- * array.each {|element| ... } -> self
- * array.each -> Enumerator
+ * each {|element| ... } -> self
+ * each -> new_enumerator
*
- * Iterates over array elements.
- *
- * When a block given, passes each successive array element to the block;
+ * With a block given, iterates over the elements of +self+,
+ * passing each element to the block;
* returns +self+:
*
* a = [:foo, 'bar', 2]
@@ -2501,20 +2735,9 @@ ary_enum_length(VALUE ary, VALUE args, VALUE eobj)
* foo
* bar
*
- * When no block given, returns a new \Enumerator:
- * a = [:foo, 'bar', 2]
- *
- * e = a.each
- * e # => #<Enumerator: [:foo, "bar", 2]:each>
- * a1 = e.each {|element| puts "#{element.class} #{element}" }
- *
- * Output:
- *
- * Symbol foo
- * String bar
- * Integer 2
+ * With no block given, returns a new Enumerator.
*
- * Related: #each_index, #reverse_each.
+ * Related: see {Methods for Iterating}[rdoc-ref:Array@Methods+for+Iterating].
*/
VALUE
@@ -2531,12 +2754,11 @@ rb_ary_each(VALUE ary)
/*
* call-seq:
- * array.each_index {|index| ... } -> self
- * array.each_index -> Enumerator
- *
- * Iterates over array indexes.
+ * each_index {|index| ... } -> self
+ * each_index -> new_enumerator
*
- * When a block given, passes each successive array index to the block;
+ * With a block given, iterates over the elements of +self+,
+ * passing each <i>array index</i> to the block;
* returns +self+:
*
* a = [:foo, 'bar', 2]
@@ -2552,26 +2774,16 @@ rb_ary_each(VALUE ary)
*
* a = [:foo, 'bar', 2]
* a.each_index {|index| puts index; a.clear if index > 0 }
+ * a # => []
*
* Output:
*
* 0
* 1
*
- * When no block given, returns a new \Enumerator:
+ * With no block given, returns a new Enumerator.
*
- * a = [:foo, 'bar', 2]
- * e = a.each_index
- * e # => #<Enumerator: [:foo, "bar", 2]:each_index>
- * a1 = e.each {|index| puts "#{index} #{a[index]}"}
- *
- * Output:
- *
- * 0 foo
- * 1 bar
- * 2 2
- *
- * Related: #each, #reverse_each.
+ * Related: see {Methods for Iterating}[rdoc-ref:Array@Methods+for+Iterating].
*/
static VALUE
@@ -2588,47 +2800,26 @@ rb_ary_each_index(VALUE ary)
/*
* call-seq:
- * array.reverse_each {|element| ... } -> self
- * array.reverse_each -> Enumerator
- *
- * Iterates backwards over array elements.
+ * reverse_each {|element| ... } -> self
+ * reverse_each -> Enumerator
*
- * When a block given, passes, in reverse order, each element to the block;
+ * When a block given, iterates backwards over the elements of +self+,
+ * passing, in reverse order, each element to the block;
* returns +self+:
*
- * a = [:foo, 'bar', 2]
- * a.reverse_each {|element| puts "#{element.class} #{element}" }
- *
- * Output:
- *
- * Integer 2
- * String bar
- * Symbol foo
+ * a = []
+ * [0, 1, 2].reverse_each {|element| a.push(element) }
+ * a # => [2, 1, 0]
*
* Allows the array to be modified during iteration:
*
- * a = [:foo, 'bar', 2]
- * a.reverse_each {|element| puts element; a.clear if element.to_s.start_with?('b') }
+ * a = ['a', 'b', 'c']
+ * a.reverse_each {|element| a.clear if element.start_with?('b') }
+ * a # => []
*
- * Output:
- *
- * 2
- * bar
- *
- * When no block given, returns a new \Enumerator:
- *
- * a = [:foo, 'bar', 2]
- * e = a.reverse_each
- * e # => #<Enumerator: [:foo, "bar", 2]:reverse_each>
- * a1 = e.each {|element| puts "#{element.class} #{element}" }
- *
- * Output:
- *
- * Integer 2
- * String bar
- * Symbol foo
+ * When no block given, returns a new Enumerator.
*
- * Related: #each, #each_index.
+ * Related: see {Methods for Iterating}[rdoc-ref:Array@Methods+for+Iterating].
*/
static VALUE
@@ -2651,9 +2842,15 @@ rb_ary_reverse_each(VALUE ary)
/*
* call-seq:
- * array.length -> an_integer
+ * length -> integer
+ * size -> integer
+ *
+ * Returns the count of elements in +self+:
*
- * Returns the count of elements in +self+.
+ * [0, 1, 2].length # => 3
+ * [].length # => 0
+ *
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -2665,10 +2862,12 @@ rb_ary_length(VALUE ary)
/*
* call-seq:
- * array.empty? -> true or false
+ * empty? -> true or false
*
* Returns +true+ if the count of elements in +self+ is zero,
* +false+ otherwise.
+ *
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -2803,23 +3002,28 @@ rb_ary_join(VALUE ary, VALUE sep)
StringValue(sep);
len += RSTRING_LEN(sep) * (RARRAY_LEN(ary) - 1);
}
- for (i=0; i<RARRAY_LEN(ary); i++) {
+ long len_memo = RARRAY_LEN(ary);
+ for (i=0; i < len_memo; i++) {
val = RARRAY_AREF(ary, i);
- tmp = rb_check_string_type(val);
-
- if (NIL_P(tmp) || tmp != val) {
- int first;
- long n = RARRAY_LEN(ary);
- if (i > n) i = n;
- result = rb_str_buf_new(len + (n-i)*10);
- rb_enc_associate(result, rb_usascii_encoding());
- i = ary_join_0(ary, sep, i, result);
- first = i == 0;
- ary_join_1(ary, ary, sep, i, result, &first);
- return result;
+ if (RB_UNLIKELY(!RB_TYPE_P(val, T_STRING))) {
+ tmp = rb_check_string_type(val);
+ if (NIL_P(tmp) || tmp != val) {
+ int first;
+ long n = RARRAY_LEN(ary);
+ if (i > n) i = n;
+ result = rb_str_buf_new(len + (n-i)*10);
+ rb_enc_associate(result, rb_usascii_encoding());
+ i = ary_join_0(ary, sep, i, result);
+ first = i == 0;
+ ary_join_1(ary, ary, sep, i, result, &first);
+ return result;
+ }
+ len += RSTRING_LEN(tmp);
+ len_memo = RARRAY_LEN(ary);
+ }
+ else {
+ len += RSTRING_LEN(val);
}
-
- len += RSTRING_LEN(tmp);
}
result = rb_str_new(0, len);
@@ -2832,31 +3036,32 @@ rb_ary_join(VALUE ary, VALUE sep)
/*
* call-seq:
- * array.join ->new_string
- * array.join(separator = $,) -> new_string
+ * join(separator = $,) -> new_string
*
- * Returns the new \String formed by joining the array elements after conversion.
- * For each element +element+:
+ * Returns the new string formed by joining the converted elements of +self+;
+ * for each element +element+:
*
- * - Uses <tt>element.to_s</tt> if +element+ is not a <tt>kind_of?(Array)</tt>.
- * - Uses recursive <tt>element.join(separator)</tt> if +element+ is a <tt>kind_of?(Array)</tt>.
+ * - Converts recursively using <tt>element.join(separator)</tt>
+ * if +element+ is a <tt>kind_of?(Array)</tt>.
+ * - Otherwise, converts using <tt>element.to_s</tt>.
*
- * With no argument, joins using the output field separator, <tt>$,</tt>:
+ * With no argument given, joins using the output field separator, <tt>$,</tt>:
*
* a = [:foo, 'bar', 2]
* $, # => nil
* a.join # => "foobar2"
*
- * With \string argument +separator+, joins using that separator:
+ * With string argument +separator+ given, joins using that separator:
*
* a = [:foo, 'bar', 2]
* a.join("\n") # => "foo\nbar\n2"
*
- * Joins recursively for nested Arrays:
+ * Joins recursively for nested arrays:
*
* a = [:foo, [:bar, [:baz, :bat]]]
* a.join # => "foobarbazbat"
*
+ * Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/
static VALUE
rb_ary_join_m(int argc, VALUE *argv, VALUE ary)
@@ -2893,14 +3098,16 @@ inspect_ary(VALUE ary, VALUE dummy, int recur)
/*
* call-seq:
- * array.inspect -> new_string
+ * inspect -> new_string
+ * to_s -> new_string
*
- * Returns the new \String formed by calling method <tt>#inspect</tt>
+ * Returns the new string formed by calling method <tt>#inspect</tt>
* on each array element:
*
* a = [:foo, 'bar', 2]
* a.inspect # => "[:foo, \"bar\", 2]"
*
+ * Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/
static VALUE
@@ -2920,21 +3127,17 @@ rb_ary_to_s(VALUE ary)
* call-seq:
* to_a -> self or new_array
*
- * When +self+ is an instance of \Array, returns +self+:
- *
- * a = [:foo, 'bar', 2]
- * a.to_a # => [:foo, "bar", 2]
+ * When +self+ is an instance of \Array, returns +self+.
*
- * Otherwise, returns a new \Array containing the elements of +self+:
+ * Otherwise, returns a new array containing the elements of +self+:
*
* class MyArray < Array; end
- * a = MyArray.new(['foo', 'bar', 'two'])
- * a.instance_of?(Array) # => false
- * a.kind_of?(Array) # => true
- * a1 = a.to_a
- * a1 # => ["foo", "bar", "two"]
- * a1.class # => Array # Not MyArray
+ * my_a = MyArray.new(['foo', 'bar', 'two'])
+ * a = my_a.to_a
+ * a # => ["foo", "bar", "two"]
+ * a.class # => Array # Not MyArray.
*
+ * Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/
static VALUE
@@ -2950,27 +3153,27 @@ rb_ary_to_a(VALUE ary)
/*
* call-seq:
- * array.to_h -> new_hash
- * array.to_h {|item| ... } -> new_hash
+ * to_h -> new_hash
+ * to_h {|element| ... } -> new_hash
*
- * Returns a new \Hash formed from +self+.
+ * Returns a new hash formed from +self+.
*
- * When a block is given, calls the block with each array element;
- * the block must return a 2-element \Array whose two elements
- * form a key-value pair in the returned \Hash:
+ * With no block given, each element of +self+ must be a 2-element sub-array;
+ * forms each sub-array into a key-value pair in the new hash:
*
- * a = ['foo', :bar, 1, [2, 3], {baz: 4}]
- * h = a.to_h {|item| [item, item] }
- * h # => {"foo"=>"foo", :bar=>:bar, 1=>1, [2, 3]=>[2, 3], {:baz=>4}=>{:baz=>4}}
+ * a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']]
+ * a.to_h # => {"foo"=>"zero", "bar"=>"one", "baz"=>"two"}
+ * [].to_h # => {}
*
- * When no block is given, +self+ must be an \Array of 2-element sub-arrays,
- * each sub-array is formed into a key-value pair in the new \Hash:
+ * With a block given, the block must return a 2-element array;
+ * calls the block with each element of +self+;
+ * forms each returned array into a key-value pair in the returned hash:
*
- * [].to_h # => {}
- * a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']]
- * h = a.to_h
- * h # => {"foo"=>"zero", "bar"=>"one", "baz"=>"two"}
+ * a = ['foo', :bar, 1, [2, 3], {baz: 4}]
+ * a.to_h {|element| [element, element.class] }
+ * # => {"foo"=>String, :bar=>Symbol, 1=>Integer, [2, 3]=>Array, {:baz=>4}=>Hash}
*
+ * Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/
static VALUE
@@ -2999,7 +3202,7 @@ rb_ary_to_h(VALUE ary)
/*
* call-seq:
- * array.to_ary -> self
+ * to_ary -> self
*
* Returns +self+.
*/
@@ -3038,13 +3241,16 @@ rb_ary_reverse(VALUE ary)
/*
* call-seq:
- * array.reverse! -> self
+ * reverse! -> self
*
- * Reverses +self+ in place:
+ * Reverses the order of the elements of +self+;
+ * returns +self+:
*
- * a = ['foo', 'bar', 'two']
- * a.reverse! # => ["two", "bar", "foo"]
+ * a = [0, 1, 2]
+ * a.reverse! # => [2, 1, 0]
+ * a # => [2, 1, 0]
*
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
static VALUE
@@ -3055,14 +3261,13 @@ rb_ary_reverse_bang(VALUE ary)
/*
* call-seq:
- * array.reverse -> new_array
+ * reverse -> new_array
*
- * Returns a new \Array with the elements of +self+ in reverse order:
+ * Returns a new array containing the elements of +self+ in reverse order:
*
- * a = ['foo', 'bar', 'two']
- * a1 = a.reverse
- * a1 # => ["two", "bar", "foo"]
+ * [0, 1, 2].reverse # => [2, 1, 0]
*
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
static VALUE
@@ -3124,48 +3329,34 @@ rb_ary_rotate(VALUE ary, long cnt)
/*
* call-seq:
- * array.rotate! -> self
- * array.rotate!(count) -> self
+ * rotate!(count = 1) -> self
*
* Rotates +self+ in place by moving elements from one end to the other; returns +self+.
*
- * When no argument given, rotates the first element to the last position:
- *
- * a = [:foo, 'bar', 2, 'bar']
- * a.rotate! # => ["bar", 2, "bar", :foo]
- *
- * When given a non-negative \Integer +count+,
+ * With non-negative numeric +count+,
* rotates +count+ elements from the beginning to the end:
*
- * a = [:foo, 'bar', 2]
- * a.rotate!(2)
- * a # => [2, :foo, "bar"]
+ * [0, 1, 2, 3].rotate!(2) # => [2, 3, 0, 1]
+ [0, 1, 2, 3].rotate!(2.1) # => [2, 3, 0, 1]
*
* If +count+ is large, uses <tt>count % array.size</tt> as the count:
*
- * a = [:foo, 'bar', 2]
- * a.rotate!(20)
- * a # => [2, :foo, "bar"]
+ * [0, 1, 2, 3].rotate!(21) # => [1, 2, 3, 0]
*
- * If +count+ is zero, returns +self+ unmodified:
+ * If +count+ is zero, rotates no elements:
*
- * a = [:foo, 'bar', 2]
- * a.rotate!(0)
- * a # => [:foo, "bar", 2]
+ * [0, 1, 2, 3].rotate!(0) # => [0, 1, 2, 3]
*
- * When given a negative Integer +count+, rotates in the opposite direction,
+ * With a negative numeric +count+, rotates in the opposite direction,
* from end to beginning:
*
- * a = [:foo, 'bar', 2]
- * a.rotate!(-2)
- * a # => ["bar", 2, :foo]
+ * [0, 1, 2, 3].rotate!(-1) # => [3, 0, 1, 2]
*
* If +count+ is small (far from zero), uses <tt>count % array.size</tt> as the count:
*
- * a = [:foo, 'bar', 2]
- * a.rotate!(-5)
- * a # => ["bar", 2, :foo]
+ * [0, 1, 2, 3].rotate!(-21) # => [3, 0, 1, 2]
*
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
static VALUE
@@ -3178,51 +3369,35 @@ rb_ary_rotate_bang(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.rotate -> new_array
- * array.rotate(count) -> new_array
+ * rotate(count = 1) -> new_array
*
- * Returns a new \Array formed from +self+ with elements
+ * Returns a new array formed from +self+ with elements
* rotated from one end to the other.
*
- * When no argument given, returns a new \Array that is like +self+,
- * except that the first element has been rotated to the last position:
- *
- * a = [:foo, 'bar', 2, 'bar']
- * a1 = a.rotate
- * a1 # => ["bar", 2, "bar", :foo]
+ * With non-negative numeric +count+,
+ * rotates elements from the beginning to the end:
*
- * When given a non-negative \Integer +count+,
- * returns a new \Array with +count+ elements rotated from the beginning to the end:
- *
- * a = [:foo, 'bar', 2]
- * a1 = a.rotate(2)
- * a1 # => [2, :foo, "bar"]
+ * [0, 1, 2, 3].rotate(2) # => [2, 3, 0, 1]
+ * [0, 1, 2, 3].rotate(2.1) # => [2, 3, 0, 1]
*
* If +count+ is large, uses <tt>count % array.size</tt> as the count:
*
- * a = [:foo, 'bar', 2]
- * a1 = a.rotate(20)
- * a1 # => [2, :foo, "bar"]
+ * [0, 1, 2, 3].rotate(22) # => [2, 3, 0, 1]
*
- * If +count+ is zero, returns a copy of +self+, unmodified:
+ * With a +count+ of zero, rotates no elements:
*
- * a = [:foo, 'bar', 2]
- * a1 = a.rotate(0)
- * a1 # => [:foo, "bar", 2]
+ * [0, 1, 2, 3].rotate(0) # => [0, 1, 2, 3]
*
- * When given a negative \Integer +count+, rotates in the opposite direction,
- * from end to beginning:
+ * With negative numeric +count+, rotates in the opposite direction,
+ * from the end to the beginning:
*
- * a = [:foo, 'bar', 2]
- * a1 = a.rotate(-2)
- * a1 # => ["bar", 2, :foo]
+ * [0, 1, 2, 3].rotate(-1) # => [3, 0, 1, 2]
*
* If +count+ is small (far from zero), uses <tt>count % array.size</tt> as the count:
*
- * a = [:foo, 'bar', 2]
- * a1 = a.rotate(-5)
- * a1 # => ["bar", 2, :foo]
+ * [0, 1, 2, 3].rotate(-21) # => [3, 0, 1, 2]
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -3315,50 +3490,19 @@ sort_2(const void *ap, const void *bp, void *dummy)
/*
* call-seq:
- * array.sort! -> self
- * array.sort! {|a, b| ... } -> self
- *
- * Returns +self+ with its elements sorted in place.
- *
- * With no block, compares elements using operator <tt><=></tt>
- * (see Comparable):
- *
- * a = 'abcde'.split('').shuffle
- * a # => ["e", "b", "d", "a", "c"]
- * a.sort!
- * a # => ["a", "b", "c", "d", "e"]
- *
- * With a block, calls the block with each element pair;
- * for each element pair +a+ and +b+, the block should return an integer:
+ * sort! -> self
+ * sort! {|a, b| ... } -> self
*
- * - Negative when +b+ is to follow +a+.
- * - Zero when +a+ and +b+ are equivalent.
- * - Positive when +a+ is to follow +b+.
- *
- * Example:
- *
- * a = 'abcde'.split('').shuffle
- * a # => ["e", "b", "d", "a", "c"]
- * a.sort! {|a, b| a <=> b }
- * a # => ["a", "b", "c", "d", "e"]
- * a.sort! {|a, b| b <=> a }
- * a # => ["e", "d", "c", "b", "a"]
- *
- * When the block returns zero, the order for +a+ and +b+ is indeterminate,
- * and may be unstable:
- *
- * a = 'abcde'.split('').shuffle
- * a # => ["e", "b", "d", "a", "c"]
- * a.sort! {|a, b| 0 }
- * a # => ["d", "e", "c", "a", "b"]
+ * Like Array#sort, but returns +self+ with its elements sorted in place.
*
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
VALUE
rb_ary_sort_bang(VALUE ary)
{
rb_ary_modify(ary);
- assert(!ARY_SHARED_P(ary));
+ RUBY_ASSERT(!ARY_SHARED_P(ary));
if (RARRAY_LEN(ary) > 1) {
VALUE tmp = ary_make_substitution(ary); /* only ary refers tmp */
struct ary_sort_data data;
@@ -3376,6 +3520,9 @@ rb_ary_sort_bang(VALUE ary)
rb_ary_unshare(ary);
FL_SET_EMBED(ary);
}
+ if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) {
+ ary_resize_capa(ary, ARY_EMBED_LEN(tmp));
+ }
ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp));
ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
}
@@ -3385,7 +3532,7 @@ rb_ary_sort_bang(VALUE ary)
ARY_SET_CAPA(ary, RARRAY_LEN(tmp));
}
else {
- assert(!ARY_SHARED_P(tmp));
+ RUBY_ASSERT(!ARY_SHARED_P(tmp));
if (ARY_EMBED_P(ary)) {
FL_UNSET_EMBED(ary);
}
@@ -3401,10 +3548,9 @@ rb_ary_sort_bang(VALUE ary)
ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
}
/* tmp was lost ownership for the ptr */
- FL_UNSET(tmp, FL_FREEZE);
FL_SET_EMBED(tmp);
ARY_SET_EMBED_LEN(tmp, 0);
- FL_SET(tmp, FL_FREEZE);
+ OBJ_FREEZE(tmp);
}
/* tmp will be GC'ed. */
RBASIC_SET_CLASS_RAW(tmp, rb_cArray); /* rb_cArray must be marked */
@@ -3415,21 +3561,18 @@ rb_ary_sort_bang(VALUE ary)
/*
* call-seq:
- * array.sort -> new_array
- * array.sort {|a, b| ... } -> new_array
+ * sort -> new_array
+ * sort {|a, b| ... } -> new_array
*
- * Returns a new \Array whose elements are those from +self+, sorted.
+ * Returns a new array containing the elements of +self+, sorted.
*
- * With no block, compares elements using operator <tt><=></tt>
- * (see Comparable):
+ * With no block given, compares elements using operator <tt>#<=></tt>
+ * (see Object#<=>):
*
- * a = 'abcde'.split('').shuffle
- * a # => ["e", "b", "d", "a", "c"]
- * a1 = a.sort
- * a1 # => ["a", "b", "c", "d", "e"]
+ * [0, 2, 3, 1].sort # => [0, 1, 2, 3]
*
- * With a block, calls the block with each element pair;
- * for each element pair +a+ and +b+, the block should return an integer:
+ * With a block given, calls the block with each combination of pairs of elements from +self+;
+ * for each pair +a+ and +b+, the block should return a numeric:
*
* - Negative when +b+ is to follow +a+.
* - Zero when +a+ and +b+ are equivalent.
@@ -3437,22 +3580,17 @@ rb_ary_sort_bang(VALUE ary)
*
* Example:
*
- * a = 'abcde'.split('').shuffle
- * a # => ["e", "b", "d", "a", "c"]
- * a1 = a.sort {|a, b| a <=> b }
- * a1 # => ["a", "b", "c", "d", "e"]
- * a2 = a.sort {|a, b| b <=> a }
- * a2 # => ["e", "d", "c", "b", "a"]
+ * a = [3, 2, 0, 1]
+ * a.sort {|a, b| a <=> b } # => [0, 1, 2, 3]
+ * a.sort {|a, b| b <=> a } # => [3, 2, 1, 0]
*
* When the block returns zero, the order for +a+ and +b+ is indeterminate,
- * and may be unstable:
+ * and may be unstable.
*
- * a = 'abcde'.split('').shuffle
- * a # => ["e", "b", "d", "a", "c"]
- * a1 = a.sort {|a, b| 0 }
- * a1 # => ["c", "e", "b", "d", "a"]
+ * See an example in Numeric#nonzero? for the idiom to sort more
+ * complex structure.
*
- * Related: Enumerable#sort_by.
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
VALUE
@@ -3467,12 +3605,15 @@ static VALUE rb_ary_bsearch_index(VALUE ary);
/*
* call-seq:
- * array.bsearch {|element| ... } -> object
- * array.bsearch -> new_enumerator
+ * bsearch {|element| ... } -> found_element or nil
+ * bsearch -> new_enumerator
*
- * Returns an element from +self+ selected by a binary search.
+ * Returns the element from +self+ found by a binary search,
+ * or +nil+ if the search found no suitable element.
*
- * See {Binary Searching}[rdoc-ref:bsearch.rdoc].
+ * See {Binary Searching}[rdoc-ref:language/bsearch.rdoc].
+ *
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -3488,11 +3629,15 @@ rb_ary_bsearch(VALUE ary)
/*
* call-seq:
- * array.bsearch_index {|element| ... } -> integer or nil
- * array.bsearch_index -> new_enumerator
+ * bsearch_index {|element| ... } -> integer or nil
+ * bsearch_index -> new_enumerator
+ *
+ * Returns the integer index of the element from +self+ found by a binary search,
+ * or +nil+ if the search found no suitable element.
*
- * Searches +self+ as described at method #bsearch,
- * but returns the _index_ of the found element instead of the element itself.
+ * See {Binary Searching}[rdoc-ref:language/bsearch.rdoc].
+ *
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -3522,8 +3667,8 @@ rb_ary_bsearch_index(VALUE ary)
const VALUE zero = INT2FIX(0);
switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
case 0: return INT2FIX(mid);
- case 1: smaller = 1; break;
- case -1: smaller = 0;
+ case 1: smaller = 0; break;
+ case -1: smaller = 1;
}
}
else {
@@ -3551,28 +3696,24 @@ sort_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, dummy))
/*
* call-seq:
- * array.sort_by! {|element| ... } -> self
- * array.sort_by! -> new_enumerator
+ * sort_by! {|element| ... } -> self
+ * sort_by! -> new_enumerator
*
- * Sorts the elements of +self+ in place,
- * using an ordering determined by the block; returns self.
+ * With a block given, sorts the elements of +self+ in place;
+ * returns self.
*
* Calls the block with each successive element;
- * sorts elements based on the values returned from the block.
- *
- * For duplicates returned by the block, the ordering is indeterminate, and may be unstable.
- *
- * This example sorts strings based on their sizes:
+ * sorts elements based on the values returned from the block:
*
* a = ['aaaa', 'bbb', 'cc', 'd']
* a.sort_by! {|element| element.size }
* a # => ["d", "cc", "bbb", "aaaa"]
*
- * Returns a new \Enumerator if no block given:
+ * For duplicate values returned by the block, the ordering is indeterminate, and may be unstable.
*
- * a = ['aaaa', 'bbb', 'cc', 'd']
- * a.sort_by! # => #<Enumerator: ["aaaa", "bbb", "cc", "d"]:sort_by!>
+ * With no block given, returns a new Enumerator.
*
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
static VALUE
@@ -3582,29 +3723,32 @@ rb_ary_sort_by_bang(VALUE ary)
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_modify(ary);
- sorted = rb_block_call(ary, rb_intern("sort_by"), 0, 0, sort_by_i, 0);
- rb_ary_replace(ary, sorted);
+ if (RARRAY_LEN(ary) > 1) {
+ sorted = rb_block_call(ary, rb_intern("sort_by"), 0, 0, sort_by_i, 0);
+ rb_ary_replace(ary, sorted);
+ }
return ary;
}
/*
* call-seq:
- * array.map {|element| ... } -> new_array
- * array.map -> new_enumerator
+ * collect {|element| ... } -> new_array
+ * collect -> new_enumerator
+ * map {|element| ... } -> new_array
+ * map -> new_enumerator
*
- * Calls the block, if given, with each element of +self+;
- * returns a new \Array whose elements are the return values from the block:
+ * With a block given, calls the block with each element of +self+;
+ * returns a new array whose elements are the return values from the block:
*
* a = [:foo, 'bar', 2]
* a1 = a.map {|element| element.class }
* a1 # => [Symbol, String, Integer]
*
- * Returns a new \Enumerator if no block given:
- * a = [:foo, 'bar', 2]
- * a1 = a.map
- * a1 # => #<Enumerator: [:foo, "bar", 2]:map>
+ * With no block given, returns a new Enumerator.
*
+ * Related: #collect!;
+ * see also {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/
static VALUE
@@ -3624,21 +3768,22 @@ rb_ary_collect(VALUE ary)
/*
* call-seq:
- * array.map! {|element| ... } -> self
- * array.map! -> new_enumerator
+ * collect! {|element| ... } -> self
+ * collect! -> new_enumerator
+ * map! {|element| ... } -> self
+ * map! -> new_enumerator
*
- * Calls the block, if given, with each element;
- * replaces the element with the block's return value:
+ * With a block given, calls the block with each element of +self+
+ * and replaces the element with the block's return value;
+ * returns +self+:
*
* a = [:foo, 'bar', 2]
* a.map! { |element| element.class } # => [Symbol, String, Integer]
*
- * Returns a new \Enumerator if no block given:
- *
- * a = [:foo, 'bar', 2]
- * a1 = a.map!
- * a1 # => #<Enumerator: [:foo, "bar", 2]:map!>
+ * With no block given, returns a new Enumerator.
*
+ * Related: #collect;
+ * see also {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/
static VALUE
@@ -3710,45 +3855,108 @@ append_values_at_single(VALUE result, VALUE ary, long olen, VALUE idx)
/*
* call-seq:
- * array.values_at(*indexes) -> new_array
+ * values_at(*specifiers) -> new_array
*
- * Returns a new \Array whose elements are the elements
- * of +self+ at the given \Integer or \Range +indexes+.
+ * Returns elements from +self+ in a new array; does not modify +self+.
*
- * For each positive +index+, returns the element at offset +index+:
+ * The objects included in the returned array are the elements of +self+
+ * selected by the given +specifiers+,
+ * each of which must be a numeric index or a Range.
*
- * a = [:foo, 'bar', 2]
- * a.values_at(0, 2) # => [:foo, 2]
- * a.values_at(0..1) # => [:foo, "bar"]
+ * In brief:
*
- * The given +indexes+ may be in any order, and may repeat:
+ * a = ['a', 'b', 'c', 'd']
*
- * a = [:foo, 'bar', 2]
- * a.values_at(2, 0, 1, 0, 2) # => [2, :foo, "bar", :foo, 2]
- * a.values_at(1, 0..2) # => ["bar", :foo, "bar", 2]
+ * # Index specifiers.
+ * a.values_at(2, 0, 2, 0) # => ["c", "a", "c", "a"] # May repeat.
+ * a.values_at(-4, -3, -2, -1) # => ["a", "b", "c", "d"] # Counts backwards if negative.
+ * a.values_at(-50, 50) # => [nil, nil] # Outside of self.
*
- * Assigns +nil+ for an +index+ that is too large:
+ * # Range specifiers.
+ * a.values_at(1..3) # => ["b", "c", "d"] # From range.begin to range.end.
+ * a.values_at(1...3) # => ["b", "c"] # End excluded.
+ * a.values_at(3..1) # => [] # No such elements.
*
- * a = [:foo, 'bar', 2]
- * a.values_at(0, 3, 1, 3) # => [:foo, nil, "bar", nil]
+ * a.values_at(-3..3) # => ["b", "c", "d"] # Negative range.begin counts backwards.
+ * a.values_at(-50..3) # Raises RangeError.
*
- * Returns a new empty \Array if no arguments given.
+ * a.values_at(1..-2) # => ["b", "c"] # Negative range.end counts backwards.
+ * a.values_at(1..-50) # => [] # No such elements.
*
- * For each negative +index+, counts backward from the end of the array:
+ * # Mixture of specifiers.
+ * a.values_at(2..3, 3, 0..1, 0) # => ["c", "d", "d", "a", "b", "a"]
*
- * a = [:foo, 'bar', 2]
- * a.values_at(-1, -3) # => [2, :foo]
+ * With no +specifiers+ given, returns a new empty array:
*
- * Assigns +nil+ for an +index+ that is too small:
+ * a = ['a', 'b', 'c', 'd']
+ * a.values_at # => []
*
- * a = [:foo, 'bar', 2]
- * a.values_at(0, -5, 1, -6, 2) # => [:foo, nil, "bar", nil, 2]
+ * For each numeric specifier +index+, includes an element:
*
- * The given +indexes+ may have a mixture of signs:
+ * - For each non-negative numeric specifier +index+ that is in-range (less than <tt>self.size</tt>),
+ * includes the element at offset +index+:
*
- * a = [:foo, 'bar', 2]
- * a.values_at(0, -2, 1, -1) # => [:foo, "bar", "bar", 2]
+ * a.values_at(0, 2) # => ["a", "c"]
+ * a.values_at(0.1, 2.9) # => ["a", "c"]
+ *
+ * - For each negative numeric +index+ that is in-range (greater than or equal to <tt>- self.size</tt>),
+ * counts backwards from the end of +self+:
+ *
+ * a.values_at(-1, -4) # => ["d", "a"]
+ *
+ * The given indexes may be in any order, and may repeat:
*
+ * a.values_at(2, 0, 1, 0, 2) # => ["c", "a", "b", "a", "c"]
+ *
+ * For each +index+ that is out-of-range, includes +nil+:
+ *
+ * a.values_at(4, -5) # => [nil, nil]
+ *
+ * For each Range specifier +range+, includes elements
+ * according to <tt>range.begin</tt> and <tt>range.end</tt>:
+ *
+ * - If both <tt>range.begin</tt> and <tt>range.end</tt>
+ * are non-negative and in-range (less than <tt>self.size</tt>),
+ * includes elements from index <tt>range.begin</tt>
+ * through <tt>range.end - 1</tt> (if <tt>range.exclude_end?</tt>),
+ * or through <tt>range.end</tt> (otherwise):
+ *
+ * a.values_at(1..2) # => ["b", "c"]
+ * a.values_at(1...2) # => ["b"]
+ *
+ * - If <tt>range.begin</tt> is negative and in-range (greater than or equal to <tt>- self.size</tt>),
+ * counts backwards from the end of +self+:
+ *
+ * a.values_at(-2..3) # => ["c", "d"]
+ *
+ * - If <tt>range.begin</tt> is negative and out-of-range, raises an exception:
+ *
+ * a.values_at(-5..3) # Raises RangeError.
+ *
+ * - If <tt>range.end</tt> is positive and out-of-range,
+ * extends the returned array with +nil+ elements:
+ *
+ * a.values_at(1..5) # => ["b", "c", "d", nil, nil]
+ *
+ * - If <tt>range.end</tt> is negative and in-range,
+ * counts backwards from the end of +self+:
+ *
+ * a.values_at(1..-2) # => ["b", "c"]
+ *
+ * - If <tt>range.end</tt> is negative and out-of-range,
+ * returns an empty array:
+ *
+ * a.values_at(1..-5) # => []
+ *
+ * The given ranges may be in any order and may repeat:
+ *
+ * a.values_at(2..3, 0..1, 2..3) # => ["c", "d", "a", "b", "c", "d"]
+ *
+ * The given specifiers may be any mixture of indexes and ranges:
+ *
+ * a.values_at(3, 1..2, 0, 2..3) # => ["d", "b", "c", "a", "c", "d"]
+ *
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -3766,22 +3974,22 @@ rb_ary_values_at(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.select {|element| ... } -> new_array
- * array.select -> new_enumerator
+ * select {|element| ... } -> new_array
+ * select -> new_enumerator
+ * filter {|element| ... } -> new_array
+ * filter -> new_enumerator
*
- * Calls the block, if given, with each element of +self+;
- * returns a new \Array containing those elements of +self+
+ * With a block given, calls the block with each element of +self+;
+ * returns a new array containing those elements of +self+
* for which the block returns a truthy value:
*
* a = [:foo, 'bar', 2, :bam]
- * a1 = a.select {|element| element.to_s.start_with?('b') }
- * a1 # => ["bar", :bam]
- *
- * Returns a new \Enumerator if no block given:
+ * a.select {|element| element.to_s.start_with?('b') }
+ * # => ["bar", :bam]
*
- * a = [:foo, 'bar', 2, :bam]
- * a.select # => #<Enumerator: [:foo, "bar", 2, :bam]:select>
+ * With no block given, returns a new Enumerator.
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -3847,10 +4055,12 @@ select_bang_ensure(VALUE a)
/*
* call-seq:
- * array.select! {|element| ... } -> self or nil
- * array.select! -> new_enumerator
+ * select! {|element| ... } -> self or nil
+ * select! -> new_enumerator
+ * filter! {|element| ... } -> self or nil
+ * filter! -> new_enumerator
*
- * Calls the block, if given with each element of +self+;
+ * With a block given, calls the block with each element of +self+;
* removes from +self+ those elements for which the block returns +false+ or +nil+.
*
* Returns +self+ if any elements were removed:
@@ -3860,11 +4070,9 @@ select_bang_ensure(VALUE a)
*
* Returns +nil+ if no elements were removed.
*
- * Returns a new \Enumerator if no block given:
- *
- * a = [:foo, 'bar', 2, :bam]
- * a.select! # => #<Enumerator: [:foo, "bar", 2, :bam]:select!>
+ * With no block given, returns a new Enumerator.
*
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
@@ -3882,20 +4090,18 @@ rb_ary_select_bang(VALUE ary)
/*
* call-seq:
- * array.keep_if {|element| ... } -> self
- * array.keep_if -> new_enumeration
+ * keep_if {|element| ... } -> self
+ * keep_if -> new_enumerator
*
- * Retains those elements for which the block returns a truthy value;
- * deletes all other elements; returns +self+:
+ * With a block given, calls the block with each element of +self+;
+ * removes the element from +self+ if the block does not return a truthy value:
*
* a = [:foo, 'bar', 2, :bam]
* a.keep_if {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
*
- * Returns a new \Enumerator if no block given:
- *
- * a = [:foo, 'bar', 2, :bam]
- * a.keep_if # => #<Enumerator: [:foo, "bar", 2, :bam]:keep_if>
+ * With no block given, returns a new Enumerator.
*
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
@@ -3921,38 +4127,39 @@ ary_resize_smaller(VALUE ary, long len)
/*
* call-seq:
- * array.delete(obj) -> deleted_object
- * array.delete(obj) {|nosuch| ... } -> deleted_object or block_return
+ * delete(object) -> last_removed_object
+ * delete(object) {|element| ... } -> last_removed_object or block_return
*
* Removes zero or more elements from +self+.
*
- * When no block is given,
- * removes from +self+ each element +ele+ such that <tt>ele == obj</tt>;
- * returns the last deleted element:
+ * With no block given,
+ * removes from +self+ each element +ele+ such that <tt>ele == object</tt>;
+ * returns the last removed element:
*
- * s1 = 'bar'; s2 = 'bar'
- * a = [:foo, s1, 2, s2]
- * a.delete('bar') # => "bar"
- * a # => [:foo, 2]
+ * a = [0, 1, 2, 2.0]
+ * a.delete(2) # => 2.0
+ * a # => [0, 1]
*
- * Returns +nil+ if no elements removed.
+ * Returns +nil+ if no elements removed:
*
- * When a block is given,
- * removes from +self+ each element +ele+ such that <tt>ele == obj</tt>.
+ * a.delete(2) # => nil
+ *
+ * With a block given,
+ * removes from +self+ each element +ele+ such that <tt>ele == object</tt>.
*
* If any such elements are found, ignores the block
- * and returns the last deleted element:
+ * and returns the last removed element:
*
- * s1 = 'bar'; s2 = 'bar'
- * a = [:foo, s1, 2, s2]
- * deleted_obj = a.delete('bar') {|obj| fail 'Cannot happen' }
- * a # => [:foo, 2]
+ * a = [0, 1, 2, 2.0]
+ * a.delete(2) {|element| fail 'Cannot happen' } # => 2.0
+ * a # => [0, 1]
*
- * If no such elements are found, returns the block's return value:
+ * If no such element is found, returns the block's return value:
*
- * a = [:foo, 'bar', 2]
- * a.delete(:nosuch) {|obj| "#{obj} not found" } # => "nosuch not found"
+ * a.delete(2) {|element| "Element #{element} not found." }
+ * # => "Element 2 not found."
*
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
VALUE
@@ -4033,9 +4240,10 @@ rb_ary_delete_at(VALUE ary, long pos)
/*
* call-seq:
- * array.delete_at(index) -> deleted_object or nil
+ * delete_at(index) -> removed_object or nil
*
- * Deletes an element from +self+, per the given \Integer +index+.
+ * Removes the element of +self+ at the given +index+, which must be an
+ * {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects].
*
* When +index+ is non-negative, deletes the element at offset +index+:
*
@@ -4043,15 +4251,19 @@ rb_ary_delete_at(VALUE ary, long pos)
* a.delete_at(1) # => "bar"
* a # => [:foo, 2]
*
- * If index is too large, returns +nil+.
- *
* When +index+ is negative, counts backward from the end of the array:
*
* a = [:foo, 'bar', 2]
* a.delete_at(-2) # => "bar"
* a # => [:foo, 2]
*
- * If +index+ is too small (far from zero), returns nil.
+ * When +index+ is out of range, returns +nil+.
+ *
+ * a = [:foo, 'bar', 2]
+ * a.delete_at(3) # => nil
+ * a.delete_at(-4) # => nil
+ *
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
@@ -4092,71 +4304,94 @@ ary_slice_bang_by_rb_ary_splice(VALUE ary, long pos, long len)
/*
* call-seq:
- * array.slice!(n) -> object or nil
- * array.slice!(start, length) -> new_array or nil
- * array.slice!(range) -> new_array or nil
+ * slice!(index) -> object or nil
+ * slice!(start, length) -> new_array or nil
+ * slice!(range) -> new_array or nil
*
* Removes and returns elements from +self+.
*
- * When the only argument is an \Integer +n+,
- * removes and returns the _nth_ element in +self+:
+ * With numeric argument +index+ given,
+ * removes and returns the element at offset +index+:
*
- * a = [:foo, 'bar', 2]
- * a.slice!(1) # => "bar"
- * a # => [:foo, 2]
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(2) # => "c"
+ * a # => ["a", "b", "d"]
+ * a.slice!(2.1) # => "d"
+ * a # => ["a", "b"]
*
- * If +n+ is negative, counts backwards from the end of +self+:
+ * If +index+ is negative, counts backwards from the end of +self+:
*
- * a = [:foo, 'bar', 2]
- * a.slice!(-1) # => 2
- * a # => [:foo, "bar"]
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(-2) # => "c"
+ * a # => ["a", "b", "d"]
*
- * If +n+ is out of range, returns +nil+.
+ * If +index+ is out of range, returns +nil+.
*
- * When the only arguments are Integers +start+ and +length+,
- * removes +length+ elements from +self+ beginning at offset +start+;
- * returns the deleted objects in a new \Array:
+ * With numeric arguments +start+ and +length+ given,
+ * removes +length+ elements from +self+ beginning at zero-based offset +start+;
+ * returns the removed objects in a new array:
*
- * a = [:foo, 'bar', 2]
- * a.slice!(0, 2) # => [:foo, "bar"]
- * a # => [2]
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(1, 2) # => ["b", "c"]
+ * a # => ["a", "d"]
+ * a.slice!(0.1, 1.1) # => ["a"]
+ * a # => ["d"]
+ *
+ * If +start+ is negative, counts backwards from the end of +self+:
+ *
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(-2, 1) # => ["c"]
+ * a # => ["a", "b", "d"]
+ *
+ * If +start+ is out-of-range, returns +nil+:
+ *
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(5, 1) # => nil
+ * a.slice!(-5, 1) # => nil
*
* If <tt>start + length</tt> exceeds the array size,
* removes and returns all elements from offset +start+ to the end:
*
- * a = [:foo, 'bar', 2]
- * a.slice!(1, 50) # => ["bar", 2]
- * a # => [:foo]
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(2, 50) # => ["c", "d"]
+ * a # => ["a", "b"]
*
* If <tt>start == a.size</tt> and +length+ is non-negative,
- * returns a new empty \Array.
+ * returns a new empty array.
*
* If +length+ is negative, returns +nil+.
*
- * When the only argument is a \Range object +range+,
- * treats <tt>range.min</tt> as +start+ above and <tt>range.size</tt> as +length+ above:
+ * With Range argument +range+ given,
+ * treats <tt>range.min</tt> as +start+ (as above)
+ * and <tt>range.size</tt> as +length+ (as above):
*
- * a = [:foo, 'bar', 2]
- * a.slice!(1..2) # => ["bar", 2]
- * a # => [:foo]
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(1..2) # => ["b", "c"]
+ * a # => ["a", "d"]
*
- * If <tt>range.start == a.size</tt>, returns a new empty \Array.
+ * If <tt>range.start == a.size</tt>, returns a new empty array:
*
- * If <tt>range.start</tt> is larger than the array size, returns +nil+.
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(4..5) # => []
*
- * If <tt>range.end</tt> is negative, counts backwards from the end of the array:
+ * If <tt>range.start</tt> is larger than the array size, returns +nil+:
*
- * a = [:foo, 'bar', 2]
- * a.slice!(0..-2) # => [:foo, "bar"]
- * a # => [2]
+ * a = ['a', 'b', 'c', 'd']
+ a.slice!(5..6) # => nil
*
* If <tt>range.start</tt> is negative,
- * calculates the start index backwards from the end of the array:
+ * calculates the start index by counting backwards from the end of +self+:
*
- * a = [:foo, 'bar', 2]
- * a.slice!(-2..2) # => ["bar", 2]
- * a # => [:foo]
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(-2..2) # => ["c"]
+ *
+ * If <tt>range.end</tt> is negative,
+ * calculates the end index by counting backwards from the end of +self+:
+ *
+ * a = ['a', 'b', 'c', 'd']
+ * a.slice!(0..-2) # => ["a", "b", "c"]
*
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
@@ -4237,10 +4472,11 @@ ary_reject_bang(VALUE ary)
/*
* call-seq:
- * array.reject! {|element| ... } -> self or nil
- * array.reject! -> new_enumerator
+ * reject! {|element| ... } -> self or nil
+ * reject! -> new_enumerator
*
- * Removes each element for which the block returns a truthy value.
+ * With a block given, calls the block with each element of +self+;
+ * removes each element for which the block returns a truthy value.
*
* Returns +self+ if any elements removed:
*
@@ -4249,11 +4485,9 @@ ary_reject_bang(VALUE ary)
*
* Returns +nil+ if no elements removed.
*
- * Returns a new \Enumerator if no block given:
- *
- * a = [:foo, 'bar', 2]
- * a.reject! # => #<Enumerator: [:foo, "bar", 2]:reject!>
+ * With no block given, returns a new Enumerator.
*
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
@@ -4266,21 +4500,19 @@ rb_ary_reject_bang(VALUE ary)
/*
* call-seq:
- * array.reject {|element| ... } -> new_array
- * array.reject -> new_enumerator
+ * reject {|element| ... } -> new_array
+ * reject -> new_enumerator
*
- * Returns a new \Array whose elements are all those from +self+
+ * With a block given, returns a new array whose elements are all those from +self+
* for which the block returns +false+ or +nil+:
*
* a = [:foo, 'bar', 2, 'bat']
* a1 = a.reject {|element| element.to_s.start_with?('b') }
* a1 # => [:foo, 2]
*
- * Returns a new \Enumerator if no block given:
- *
- * a = [:foo, 'bar', 2]
- * a.reject # => #<Enumerator: [:foo, "bar", 2]:reject>
+ * With no block given, returns a new Enumerator.
*
+ * Related: {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -4296,21 +4528,20 @@ rb_ary_reject(VALUE ary)
/*
* call-seq:
- * array.delete_if {|element| ... } -> self
- * array.delete_if -> Enumerator
+ * delete_if {|element| ... } -> self
+ * delete_if -> new_numerator
*
- * Removes each element in +self+ for which the block returns a truthy value;
+ * With a block given, calls the block with each element of +self+;
+ * removes the element if the block returns a truthy value;
* returns +self+:
*
* a = [:foo, 'bar', 2, 'bat']
* a.delete_if {|element| element.to_s.start_with?('b') } # => [:foo, 2]
*
- * Returns a new \Enumerator if no block given:
+ * With no block given, returns a new Enumerator.
*
- * a = [:foo, 'bar', 2]
- * a.delete_if # => #<Enumerator: [:foo, "bar", 2]:delete_if>
- *
-3 */
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
+ */
static VALUE
rb_ary_delete_if(VALUE ary)
@@ -4350,58 +4581,95 @@ take_items(VALUE obj, long n)
/*
* call-seq:
- * array.zip(*other_arrays) -> new_array
- * array.zip(*other_arrays) {|other_array| ... } -> nil
+ * zip(*other_arrays) -> new_array
+ * zip(*other_arrays) {|sub_array| ... } -> nil
+ *
+ * With no block given, combines +self+ with the collection of +other_arrays+;
+ * returns a new array of sub-arrays:
*
- * When no block given, returns a new \Array +new_array+ of size <tt>self.size</tt>
- * whose elements are Arrays.
+ * [0, 1].zip(['zero', 'one'], [:zero, :one])
+ * # => [[0, "zero", :zero], [1, "one", :one]]
*
- * Each nested array <tt>new_array[n]</tt> is of size <tt>other_arrays.size+1</tt>,
- * and contains:
+ * Returned:
*
- * - The _nth_ element of +self+.
- * - The _nth_ element of each of the +other_arrays+.
+ * - The outer array is of size <tt>self.size</tt>.
+ * - Each sub-array is of size <tt>other_arrays.size + 1</tt>.
+ * - The _nth_ sub-array contains (in order):
*
- * If all +other_arrays+ and +self+ are the same size:
+ * - The _nth_ element of +self+.
+ * - The _nth_ element of each of the other arrays, as available.
+ *
+ * Example:
+ *
+ * a = [0, 1]
+ * zipped = a.zip(['zero', 'one'], [:zero, :one])
+ * # => [[0, "zero", :zero], [1, "one", :one]]
+ * zipped.size # => 2 # Same size as a.
+ * zipped.first.size # => 3 # Size of other arrays plus 1.
+ *
+ * When the other arrays are all the same size as +self+,
+ * the returned sub-arrays are a rearrangement containing exactly elements of all the arrays
+ * (including +self+), with no omissions or additions:
*
* a = [:a0, :a1, :a2, :a3]
* b = [:b0, :b1, :b2, :b3]
* c = [:c0, :c1, :c2, :c3]
* d = a.zip(b, c)
- * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
+ * pp d
+ * # =>
+ * [[:a0, :b0, :c0],
+ * [:a1, :b1, :c1],
+ * [:a2, :b2, :c2],
+ * [:a3, :b3, :c3]]
*
- * If any array in +other_arrays+ is smaller than +self+,
- * fills to <tt>self.size</tt> with +nil+:
+ * When one of the other arrays is smaller than +self+,
+ * pads the corresponding sub-array with +nil+ elements:
*
* a = [:a0, :a1, :a2, :a3]
* b = [:b0, :b1, :b2]
* c = [:c0, :c1]
* d = a.zip(b, c)
- * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
+ * pp d
+ * # =>
+ * [[:a0, :b0, :c0],
+ * [:a1, :b1, :c1],
+ * [:a2, :b2, nil],
+ * [:a3, nil, nil]]
*
- * If any array in +other_arrays+ is larger than +self+,
- * its trailing elements are ignored:
+ * When one of the other arrays is larger than +self+,
+ * _ignores_ its trailing elements:
*
* a = [:a0, :a1, :a2, :a3]
* b = [:b0, :b1, :b2, :b3, :b4]
* c = [:c0, :c1, :c2, :c3, :c4, :c5]
* d = a.zip(b, c)
- * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
+ * pp d
+ * # =>
+ * [[:a0, :b0, :c0],
+ * [:a1, :b1, :c1],
+ * [:a2, :b2, :c2],
+ * [:a3, :b3, :c3]]
*
- * When a block is given, calls the block with each of the sub-arrays (formed as above); returns +nil+:
+ * With a block given, calls the block with each of the other arrays;
+ * returns +nil+:
*
+ * d = []
* a = [:a0, :a1, :a2, :a3]
* b = [:b0, :b1, :b2, :b3]
* c = [:c0, :c1, :c2, :c3]
- * a.zip(b, c) {|sub_array| p sub_array} # => nil
- *
- * Output:
- *
- * [:a0, :b0, :c0]
- * [:a1, :b1, :c1]
- * [:a2, :b2, :c2]
- * [:a3, :b3, :c3]
- *
+ * a.zip(b, c) {|sub_array| d.push(sub_array.reverse) } # => nil
+ * pp d
+ * # =>
+ * [[:c0, :b0, :a0],
+ * [:c1, :b1, :a1],
+ * [:c2, :b2, :a2],
+ * [:c3, :b3, :a3]]
+ *
+ * For an *object* in *other_arrays* that is not actually an array,
+ * forms the "other array" as <tt>object.to_ary</tt>, if defined,
+ * or as <tt>object.each.to_a</tt> otherwise.
+ *
+ * Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/
static VALUE
@@ -4464,14 +4732,17 @@ rb_ary_zip(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.transpose -> new_array
+ * transpose -> new_array
*
- * Transposes the rows and columns in an \Array of Arrays;
- * the nested Arrays must all be the same size:
+ * Returns a new array that is +self+
+ * as a {transposed matrix}[https://en.wikipedia.org/wiki/Transpose]:
*
* a = [[:a0, :a1], [:b0, :b1], [:c0, :c1]]
* a.transpose # => [[:a0, :b0, :c0], [:a1, :b1, :c1]]
*
+ * The elements of +self+ must all be the same size.
+ *
+ * Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/
static VALUE
@@ -4504,13 +4775,17 @@ rb_ary_transpose(VALUE ary)
/*
* call-seq:
- * array.replace(other_array) -> self
+ * initialize_copy(other_array) -> self
+ * replace(other_array) -> self
*
- * Replaces the content of +self+ with the content of +other_array+; returns +self+:
+ * Replaces the elements of +self+ with the elements of +other_array+, which must be an
+ * {array-convertible object}[rdoc-ref:implicit_conversion.rdoc@Array-Convertible+Objects];
+ * returns +self+:
*
- * a = [:foo, 'bar', 2]
- * a.replace(['foo', :bar, 3]) # => ["foo", :bar, 3]
+ * a = ['a', 'b', 'c'] # => ["a", "b", "c"]
+ * a.replace(['d', 'e']) # => ["d", "e"]
*
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
VALUE
@@ -4524,7 +4799,7 @@ rb_ary_replace(VALUE copy, VALUE orig)
/* orig has enough space to embed the contents of orig. */
if (RARRAY_LEN(orig) <= ary_embed_capa(copy)) {
- assert(ARY_EMBED_P(copy));
+ RUBY_ASSERT(ARY_EMBED_P(copy));
ary_memcpy(copy, 0, RARRAY_LEN(orig), RARRAY_CONST_PTR(orig));
ARY_SET_EMBED_LEN(copy, RARRAY_LEN(orig));
}
@@ -4532,7 +4807,7 @@ rb_ary_replace(VALUE copy, VALUE orig)
* contents of orig. */
else if (ARY_EMBED_P(orig)) {
long len = ARY_EMBED_LEN(orig);
- VALUE *ptr = ary_heap_alloc(len);
+ VALUE *ptr = ary_heap_alloc_buffer(len);
FL_UNSET_EMBED(copy);
ARY_SET_PTR(copy, ptr);
@@ -4551,6 +4826,8 @@ rb_ary_replace(VALUE copy, VALUE orig)
ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
rb_ary_set_shared(copy, shared_root);
+
+ RUBY_ASSERT(RB_OBJ_SHAREABLE_P(copy) ? RB_OBJ_SHAREABLE_P(shared_root) : 1);
}
ary_verify(copy);
return copy;
@@ -4558,13 +4835,14 @@ rb_ary_replace(VALUE copy, VALUE orig)
/*
* call-seq:
- * array.clear -> self
+ * clear -> self
*
- * Removes all elements from +self+:
+ * Removes all elements from +self+; returns +self+:
*
* a = [:foo, 'bar', 2]
* a.clear # => []
*
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
VALUE
@@ -4572,11 +4850,9 @@ rb_ary_clear(VALUE ary)
{
rb_ary_modify_check(ary);
if (ARY_SHARED_P(ary)) {
- if (!ARY_EMBED_P(ary)) {
- rb_ary_unshare(ary);
- FL_SET_EMBED(ary);
- ARY_SET_EMBED_LEN(ary, 0);
- }
+ rb_ary_unshare(ary);
+ FL_SET_EMBED(ary);
+ ARY_SET_EMBED_LEN(ary, 0);
}
else {
ARY_SET_LEN(ary, 0);
@@ -4590,198 +4866,182 @@ rb_ary_clear(VALUE ary)
/*
* call-seq:
- * array.fill(obj) -> self
- * array.fill(obj, start) -> self
- * array.fill(obj, start, length) -> self
- * array.fill(obj, range) -> self
- * array.fill {|index| ... } -> self
- * array.fill(start) {|index| ... } -> self
- * array.fill(start, length) {|index| ... } -> self
- * array.fill(range) {|index| ... } -> self
+ * fill(object, start = nil, count = nil) -> self
+ * fill(object, range) -> self
+ * fill(start = nil, count = nil) {|element| ... } -> self
+ * fill(range) {|element| ... } -> self
*
- * Replaces specified elements in +self+ with specified objects; returns +self+.
+ * Replaces selected elements in +self+;
+ * may add elements to +self+;
+ * always returns +self+ (never a new array).
*
- * With argument +obj+ and no block given, replaces all elements with that one object:
+ * In brief:
*
- * a = ['a', 'b', 'c', 'd']
- * a # => ["a", "b", "c", "d"]
- * a.fill(:X) # => [:X, :X, :X, :X]
+ * # Non-negative start.
+ * ['a', 'b', 'c', 'd'].fill('-', 1, 2) # => ["a", "-", "-", "d"]
+ * ['a', 'b', 'c', 'd'].fill(1, 2) {|e| e.to_s } # => ["a", "1", "2", "d"]
*
- * With arguments +obj+ and \Integer +start+, and no block given,
- * replaces elements based on the given start.
+ * # Extends with specified values if necessary.
+ * ['a', 'b', 'c', 'd'].fill('-', 3, 2) # => ["a", "b", "c", "-", "-"]
+ * ['a', 'b', 'c', 'd'].fill(3, 2) {|e| e.to_s } # => ["a", "b", "c", "3", "4"]
*
- * If +start+ is in range (<tt>0 <= start < array.size</tt>),
- * replaces all elements from offset +start+ through the end:
+ * # Fills with nils if necessary.
+ * ['a', 'b', 'c', 'd'].fill('-', 6, 2) # => ["a", "b", "c", "d", nil, nil, "-", "-"]
+ * ['a', 'b', 'c', 'd'].fill(6, 2) {|e| e.to_s } # => ["a", "b", "c", "d", nil, nil, "6", "7"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, 2) # => ["a", "b", :X, :X]
+ * # For negative start, counts backwards from the end.
+ * ['a', 'b', 'c', 'd'].fill('-', -3, 3) # => ["a", "-", "-", "-"]
+ * ['a', 'b', 'c', 'd'].fill(-3, 3) {|e| e.to_s } # => ["a", "1", "2", "3"]
*
- * If +start+ is too large (<tt>start >= array.size</tt>), does nothing:
+ * # Range.
+ * ['a', 'b', 'c', 'd'].fill('-', 1..2) # => ["a", "-", "-", "d"]
+ * ['a', 'b', 'c', 'd'].fill(1..2) {|e| e.to_s } # => ["a", "1", "2", "d"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, 4) # => ["a", "b", "c", "d"]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, 5) # => ["a", "b", "c", "d"]
+ * When arguments +start+ and +count+ are given,
+ * they select the elements of +self+ to be replaced;
+ * each must be an
+ * {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]
+ * (or +nil+):
*
- * If +start+ is negative, counts from the end (starting index is <tt>start + array.size</tt>):
+ * - +start+ specifies the zero-based offset of the first element to be replaced;
+ * +nil+ means zero.
+ * - +count+ is the number of consecutive elements to be replaced;
+ * +nil+ means "all the rest."
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, -2) # => ["a", "b", :X, :X]
+ * With argument +object+ given,
+ * that one object is used for all replacements:
*
- * If +start+ is too small (less than and far from zero), replaces all elements:
+ * o = Object.new # => #<Object:0x0000014e7bff7600>
+ * a = ['a', 'b', 'c', 'd'] # => ["a", "b", "c", "d"]
+ * a.fill(o, 1, 2)
+ * # => ["a", #<Object:0x0000014e7bff7600>, #<Object:0x0000014e7bff7600>, "d"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, -6) # => [:X, :X, :X, :X]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, -50) # => [:X, :X, :X, :X]
+ * With a block given, the block is called once for each element to be replaced;
+ * the value passed to the block is the _index_ of the element to be replaced
+ * (not the element itself);
+ * the block's return value replaces the element:
*
- * With arguments +obj+, \Integer +start+, and \Integer +length+, and no block given,
- * replaces elements based on the given +start+ and +length+.
+ * a = ['a', 'b', 'c', 'd'] # => ["a", "b", "c", "d"]
+ * a.fill(1, 2) {|element| element.to_s } # => ["a", "1", "2", "d"]
*
- * If +start+ is in range, replaces +length+ elements beginning at offset +start+:
+ * For arguments +start+ and +count+:
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, 1, 1) # => ["a", :X, "c", "d"]
+ * - If +start+ is non-negative,
+ * replaces +count+ elements beginning at offset +start+:
*
- * If +start+ is negative, counts from the end:
+ * ['a', 'b', 'c', 'd'].fill('-', 0, 2) # => ["-", "-", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', 1, 2) # => ["a", "-", "-", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', 2, 2) # => ["a", "b", "-", "-"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, -2, 1) # => ["a", "b", :X, "d"]
+ * ['a', 'b', 'c', 'd'].fill(0, 2) {|e| e.to_s } # => ["0", "1", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill(1, 2) {|e| e.to_s } # => ["a", "1", "2", "d"]
+ * ['a', 'b', 'c', 'd'].fill(2, 2) {|e| e.to_s } # => ["a", "b", "2", "3"]
*
- * If +start+ is large (<tt>start >= array.size</tt>), extends +self+ with +nil+:
+ * Extends +self+ if necessary:
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, 5, 0) # => ["a", "b", "c", "d", nil]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, 5, 2) # => ["a", "b", "c", "d", nil, :X, :X]
+ * ['a', 'b', 'c', 'd'].fill('-', 3, 2) # => ["a", "b", "c", "-", "-"]
+ * ['a', 'b', 'c', 'd'].fill('-', 4, 2) # => ["a", "b", "c", "d", "-", "-"]
*
- * If +length+ is zero or negative, replaces no elements:
+ * ['a', 'b', 'c', 'd'].fill(3, 2) {|e| e.to_s } # => ["a", "b", "c", "3", "4"]
+ * ['a', 'b', 'c', 'd'].fill(4, 2) {|e| e.to_s } # => ["a", "b", "c", "d", "4", "5"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, 1, 0) # => ["a", "b", "c", "d"]
- * a.fill(:X, 1, -1) # => ["a", "b", "c", "d"]
+ * Fills with +nil+ if necessary:
*
- * With arguments +obj+ and \Range +range+, and no block given,
- * replaces elements based on the given range.
+ * ['a', 'b', 'c', 'd'].fill('-', 5, 2) # => ["a", "b", "c", "d", nil, "-", "-"]
+ * ['a', 'b', 'c', 'd'].fill('-', 6, 2) # => ["a", "b", "c", "d", nil, nil, "-", "-"]
*
- * If the range is positive and ascending (<tt>0 < range.begin <= range.end</tt>),
- * replaces elements from <tt>range.begin</tt> to <tt>range.end</tt>:
+ * ['a', 'b', 'c', 'd'].fill(5, 2) {|e| e.to_s } # => ["a", "b", "c", "d", nil, "5", "6"]
+ * ['a', 'b', 'c', 'd'].fill(6, 2) {|e| e.to_s } # => ["a", "b", "c", "d", nil, nil, "6", "7"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, (1..1)) # => ["a", :X, "c", "d"]
+ * Does nothing if +count+ is non-positive:
*
- * If <tt>range.first</tt> is negative, replaces no elements:
+ * ['a', 'b', 'c', 'd'].fill('-', 2, 0) # => ["a", "b", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', 2, -100) # => ["a", "b", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', 6, -100) # => ["a", "b", "c", "d"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, (-1..1)) # => ["a", "b", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill(2, 0) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill(2, -100) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill(6, -100) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
*
- * If <tt>range.last</tt> is negative, counts from the end:
+ * - If +start+ is negative, counts backwards from the end of +self+:
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, (0..-2)) # => [:X, :X, :X, "d"]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, (1..-2)) # => ["a", :X, :X, "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', -4, 3) # => ["-", "-", "-", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', -3, 3) # => ["a", "-", "-", "-"]
*
- * If <tt>range.last</tt> and <tt>range.last</tt> are both negative,
- * both count from the end of the array:
+ * ['a', 'b', 'c', 'd'].fill(-4, 3) {|e| e.to_s } # => ["0", "1", "2", "d"]
+ * ['a', 'b', 'c', 'd'].fill(-3, 3) {|e| e.to_s } # => ["a", "1", "2", "3"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, (-1..-1)) # => ["a", "b", "c", :X]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(:X, (-2..-2)) # => ["a", "b", :X, "d"]
+ * Extends +self+ if necessary:
*
- * With no arguments and a block given, calls the block with each index;
- * replaces the corresponding element with the block's return value:
+ * ['a', 'b', 'c', 'd'].fill('-', -2, 3) # => ["a", "b", "-", "-", "-"]
+ * ['a', 'b', 'c', 'd'].fill('-', -1, 3) # => ["a", "b", "c", "-", "-", "-"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "new_3"]
+ * ['a', 'b', 'c', 'd'].fill(-2, 3) {|e| e.to_s } # => ["a", "b", "2", "3", "4"]
+ * ['a', 'b', 'c', 'd'].fill(-1, 3) {|e| e.to_s } # => ["a", "b", "c", "3", "4", "5"]
*
- * With argument +start+ and a block given, calls the block with each index
- * from offset +start+ to the end; replaces the corresponding element
- * with the block's return value.
+ * Starts at the beginning of +self+ if +start+ is negative and out-of-range:
*
- * If start is in range (<tt>0 <= start < array.size</tt>),
- * replaces from offset +start+ to the end:
+ * ['a', 'b', 'c', 'd'].fill('-', -5, 2) # => ["-", "-", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', -6, 2) # => ["-", "-", "c", "d"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(1) { |index| "new_#{index}" } # => ["a", "new_1", "new_2", "new_3"]
+ * ['a', 'b', 'c', 'd'].fill(-5, 2) {|e| e.to_s } # => ["0", "1", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill(-6, 2) {|e| e.to_s } # => ["0", "1", "c", "d"]
*
- * If +start+ is too large(<tt>start >= array.size</tt>), does nothing:
+ * Does nothing if +count+ is non-positive:
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(4) { |index| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(4) { |index| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', -2, 0) # => ["a", "b", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', -2, -1) # => ["a", "b", "c", "d"]
*
- * If +start+ is negative, counts from the end:
+ * ['a', 'b', 'c', 'd'].fill(-2, 0) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill(-2, -1) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(-2) { |index| "new_#{index}" } # => ["a", "b", "new_2", "new_3"]
+ * When argument +range+ is given,
+ * it must be a Range object whose members are numeric;
+ * its +begin+ and +end+ values determine the elements of +self+
+ * to be replaced:
*
- * If start is too small (<tt>start <= -array.size</tt>, replaces all elements:
+ * - If both +begin+ and +end+ are positive, they specify the first and last elements
+ * to be replaced:
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(-6) { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "new_3"]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(-50) { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "new_3"]
+ * ['a', 'b', 'c', 'd'].fill('-', 1..2) # => ["a", "-", "-", "d"]
+ * ['a', 'b', 'c', 'd'].fill(1..2) {|e| e.to_s } # => ["a", "1", "2", "d"]
*
- * With arguments +start+ and +length+, and a block given,
- * calls the block for each index specified by start length;
- * replaces the corresponding element with the block's return value.
+ * If +end+ is smaller than +begin+, replaces no elements:
*
- * If +start+ is in range, replaces +length+ elements beginning at offset +start+:
+ * ['a', 'b', 'c', 'd'].fill('-', 2..1) # => ["a", "b", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill(2..1) {|e| e.to_s } # => ["a", "b", "c", "d"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(1, 1) { |index| "new_#{index}" } # => ["a", "new_1", "c", "d"]
- *
- * If start is negative, counts from the end:
- *
- * a = ['a', 'b', 'c', 'd']
- * a.fill(-2, 1) { |index| "new_#{index}" } # => ["a", "b", "new_2", "d"]
- *
- * If +start+ is large (<tt>start >= array.size</tt>), extends +self+ with +nil+:
+ * - If either is negative (or both are negative), counts backwards from the end of +self+:
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(5, 0) { |index| "new_#{index}" } # => ["a", "b", "c", "d", nil]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(5, 2) { |index| "new_#{index}" } # => ["a", "b", "c", "d", nil, "new_5", "new_6"]
+ * ['a', 'b', 'c', 'd'].fill('-', -3..2) # => ["a", "-", "-", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', 1..-2) # => ["a", "-", "-", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', -3..-2) # => ["a", "-", "-", "d"]
*
- * If +length+ is zero or less, replaces no elements:
+ * ['a', 'b', 'c', 'd'].fill(-3..2) {|e| e.to_s } # => ["a", "1", "2", "d"]
+ * ['a', 'b', 'c', 'd'].fill(1..-2) {|e| e.to_s } # => ["a", "1", "2", "d"]
+ * ['a', 'b', 'c', 'd'].fill(-3..-2) {|e| e.to_s } # => ["a", "1", "2", "d"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(1, 0) { |index| "new_#{index}" } # => ["a", "b", "c", "d"]
- * a.fill(1, -1) { |index| "new_#{index}" } # => ["a", "b", "c", "d"]
+ * - If the +end+ value is excluded (see Range#exclude_end?), omits the last replacement:
*
- * With arguments +obj+ and +range+, and a block given,
- * calls the block with each index in the given range;
- * replaces the corresponding element with the block's return value.
+ * ['a', 'b', 'c', 'd'].fill('-', 1...2) # => ["a", "-", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', 1...-2) # => ["a", "-", "c", "d"]
*
- * If the range is positive and ascending (<tt>range 0 < range.begin <= range.end</tt>,
- * replaces elements from <tt>range.begin</tt> to <tt>range.end</tt>:
+ * ['a', 'b', 'c', 'd'].fill(1...2) {|e| e.to_s } # => ["a", "1", "c", "d"]
+ * ['a', 'b', 'c', 'd'].fill(1...-2) {|e| e.to_s } # => ["a", "1", "c", "d"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(1..1) { |index| "new_#{index}" } # => ["a", "new_1", "c", "d"]
+ * - If the range is endless (see {Endless Ranges}[rdoc-ref:Range@Endless+Ranges]),
+ * replaces elements to the end of +self+:
*
- * If +range.first+ is negative, does nothing:
+ * ['a', 'b', 'c', 'd'].fill('-', 1..) # => ["a", "-", "-", "-"]
+ * ['a', 'b', 'c', 'd'].fill(1..) {|e| e.to_s } # => ["a", "1", "2", "3"]
*
- * a = ['a', 'b', 'c', 'd']
- * a.fill(-1..1) { |index| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
+ * - If the range is beginless (see {Beginless Ranges}[rdoc-ref:Range@Beginless+Ranges]),
+ * replaces elements from the beginning of +self+:
*
- * If <tt>range.last</tt> is negative, counts from the end:
- *
- * a = ['a', 'b', 'c', 'd']
- * a.fill(0..-2) { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "d"]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(1..-2) { |index| "new_#{index}" } # => ["a", "new_1", "new_2", "d"]
- *
- * If <tt>range.first</tt> and <tt>range.last</tt> are both negative,
- * both count from the end:
- *
- * a = ['a', 'b', 'c', 'd']
- * a.fill(-1..-1) { |index| "new_#{index}" } # => ["a", "b", "c", "new_3"]
- * a = ['a', 'b', 'c', 'd']
- * a.fill(-2..-2) { |index| "new_#{index}" } # => ["a", "b", "new_2", "d"]
+ * ['a', 'b', 'c', 'd'].fill('-', ..2) # => ["-", "-", "-", "d"]
+ * ['a', 'b', 'c', 'd'].fill(..2) {|e| e.to_s } # => ["0", "1", "2", "d"]
*
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
static VALUE
@@ -4850,15 +5110,15 @@ rb_ary_fill(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array + other_array -> new_array
+ * self + other_array -> new_array
*
- * Returns a new \Array containing all elements of +array+
+ * Returns a new array containing all elements of +self+
* followed by all elements of +other_array+:
*
* a = [0, 1] + [2, 3]
* a # => [0, 1, 2, 3]
*
- * Related: #concat.
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
VALUE
@@ -4892,12 +5152,15 @@ ary_append(VALUE x, VALUE y)
/*
* call-seq:
- * array.concat(*other_arrays) -> self
+ * concat(*other_arrays) -> self
*
- * Adds to +array+ all elements from each \Array in +other_arrays+; returns +self+:
+ * Adds to +self+ all elements from each array in +other_arrays+; returns +self+:
*
* a = [0, 1]
- * a.concat([2, 3], [4, 5]) # => [0, 1, 2, 3, 4, 5]
+ * a.concat(['two', 'three'], [:four, :five], a)
+ * # => [0, 1, "two", "three", :four, :five, 0, 1]
+ *
+ * Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
static VALUE
@@ -4929,19 +5192,19 @@ rb_ary_concat(VALUE x, VALUE y)
/*
* call-seq:
- * array * n -> new_array
- * array * string_separator -> new_string
+ * self * n -> new_array
+ * self * string_separator -> new_string
*
- * When non-negative argument \Integer +n+ is given,
- * returns a new \Array built by concatenating the +n+ copies of +self+:
+ * When non-negative integer argument +n+ is given,
+ * returns a new array built by concatenating +n+ copies of +self+:
*
* a = ['x', 'y']
* a * 3 # => ["x", "y", "x", "y", "x", "y"]
*
- * When \String argument +string_separator+ is given,
- * equivalent to <tt>array.join(string_separator)</tt>:
+ * When string argument +string_separator+ is given,
+ * equivalent to <tt>self.join(string_separator)</tt>:
*
- * [0, [0, 1], {foo: 0}] * ', ' # => "0, 0, 1, {:foo=>0}"
+ * [0, [0, 1], {foo: 0}] * ', ' # => "0, 0, 1, {foo: 0}"
*
*/
@@ -4991,17 +5254,18 @@ rb_ary_times(VALUE ary, VALUE times)
/*
* call-seq:
- * array.assoc(obj) -> found_array or nil
+ * assoc(object) -> found_array or nil
*
- * Returns the first element in +self+ that is an \Array
- * whose first element <tt>==</tt> +obj+:
+ * Returns the first element +ele+ in +self+ such that +ele+ is an array
+ * and <tt>ele[0] == object</tt>:
*
* a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]]
* a.assoc(4) # => [4, 5, 6]
*
* Returns +nil+ if no such element is found.
*
- * Related: #rassoc.
+ * Related: Array#rassoc;
+ * see also {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
VALUE
@@ -5021,17 +5285,19 @@ rb_ary_assoc(VALUE ary, VALUE key)
/*
* call-seq:
- * array.rassoc(obj) -> found_array or nil
+ * rassoc(object) -> found_array or nil
*
- * Returns the first element in +self+ that is an \Array
- * whose second element <tt>==</tt> +obj+:
+ * Returns the first element +ele+ in +self+ such that +ele+ is an array
+ * and <tt>ele[1] == object</tt>:
*
* a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]]
* a.rassoc(4) # => [2, 4]
+ * a.rassoc(5) # => [4, 5, 6]
*
* Returns +nil+ if no such element is found.
*
- * Related: #assoc.
+ * Related: Array#assoc;
+ * see also {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
VALUE
@@ -5041,7 +5307,7 @@ rb_ary_rassoc(VALUE ary, VALUE value)
VALUE v;
for (i = 0; i < RARRAY_LEN(ary); ++i) {
- v = RARRAY_AREF(ary, i);
+ v = rb_check_array_type(RARRAY_AREF(ary, i));
if (RB_TYPE_P(v, T_ARRAY) &&
RARRAY_LEN(v) > 1 &&
rb_equal(RARRAY_AREF(v, 1), value))
@@ -5086,20 +5352,26 @@ recursive_equal(VALUE ary1, VALUE ary2, int recur)
/*
* call-seq:
- * array == other_array -> true or false
+ * self == other_array -> true or false
*
- * Returns +true+ if both <tt>array.size == other_array.size</tt>
- * and for each index +i+ in +array+, <tt>array[i] == other_array[i]</tt>:
+ * Returns whether both:
*
- * a0 = [:foo, 'bar', 2]
- * a1 = [:foo, 'bar', 2.0]
- * a1 == a0 # => true
- * [] == [] # => true
+ * - +self+ and +other_array+ are the same size.
+ * - Their corresponding elements are the same;
+ * that is, for each index +i+ in <tt>(0...self.size)</tt>,
+ * <tt>self[i] == other_array[i]</tt>.
*
- * Otherwise, returns +false+.
+ * Examples:
+ *
+ * [:foo, 'bar', 2] == [:foo, 'bar', 2] # => true
+ * [:foo, 'bar', 2] == [:foo, 'bar', 2.0] # => true
+ * [:foo, 'bar', 2] == [:foo, 'bar'] # => false # Different sizes.
+ * [:foo, 'bar', 2] == [:foo, 'bar', 3] # => false # Different elements.
*
* This method is different from method Array#eql?,
* which compares elements using <tt>Object#eql?</tt>.
+ *
+ * Related: see {Methods for Comparing}[rdoc-ref:Array@Methods+for+Comparing].
*/
static VALUE
@@ -5132,10 +5404,10 @@ recursive_eql(VALUE ary1, VALUE ary2, int recur)
/*
* call-seq:
- * array.eql? other_array -> true or false
+ * eql?(other_array) -> true or false
*
* Returns +true+ if +self+ and +other_array+ are the same size,
- * and if, for each index +i+ in +self+, <tt>self[i].eql? other_array[i]</tt>:
+ * and if, for each index +i+ in +self+, <tt>self[i].eql?(other_array[i])</tt>:
*
* a0 = [:foo, 'bar', 2]
* a1 = [:foo, 'bar', 2]
@@ -5145,6 +5417,8 @@ recursive_eql(VALUE ary1, VALUE ary2, int recur)
*
* This method is different from method Array#==,
* which compares using method <tt>Object#==</tt>.
+ *
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -5157,8 +5431,8 @@ rb_ary_eql(VALUE ary1, VALUE ary2)
return rb_exec_recursive_paired(recursive_eql, ary1, ary2, ary2);
}
-VALUE
-rb_ary_hash_values(long len, const VALUE *elements)
+static VALUE
+ary_hash_values(long len, const VALUE *elements, const VALUE ary)
{
long i;
st_index_t h;
@@ -5169,39 +5443,55 @@ rb_ary_hash_values(long len, const VALUE *elements)
for (i=0; i<len; i++) {
n = rb_hash(elements[i]);
h = rb_hash_uint(h, NUM2LONG(n));
+ if (ary) {
+ len = RARRAY_LEN(ary);
+ elements = RARRAY_CONST_PTR(ary);
+ }
}
h = rb_hash_end(h);
return ST2FIX(h);
}
+VALUE
+rb_ary_hash_values(long len, const VALUE *elements)
+{
+ return ary_hash_values(len, elements, 0);
+}
+
/*
* call-seq:
- * array.hash -> integer
+ * hash -> integer
*
* Returns the integer hash value for +self+.
*
- * Two arrays with the same content will have the same hash code (and will compare using eql?):
+ * Two arrays with the same content will have the same hash value
+ * (and will compare using eql?):
*
- * [0, 1, 2].hash == [0, 1, 2].hash # => true
- * [0, 1, 2].hash == [0, 1, 3].hash # => false
+ * ['a', 'b'].hash == ['a', 'b'].hash # => true
+ * ['a', 'b'].hash == ['a', 'c'].hash # => false
+ * ['a', 'b'].hash == ['a'].hash # => false
*
*/
static VALUE
rb_ary_hash(VALUE ary)
{
- return rb_ary_hash_values(RARRAY_LEN(ary), RARRAY_CONST_PTR(ary));
+ RBIMPL_ASSERT_OR_ASSUME(ary);
+ return ary_hash_values(RARRAY_LEN(ary), RARRAY_CONST_PTR(ary), ary);
}
/*
* call-seq:
- * array.include?(obj) -> true or false
+ * include?(object) -> true or false
*
- * Returns +true+ if for some index +i+ in +self+, <tt>obj == self[i]</tt>;
- * otherwise +false+:
+ * Returns whether for some element +element+ in +self+,
+ * <tt>object == element</tt>:
*
- * [0, 1, 2].include?(2) # => true
- * [0, 1, 2].include?(3) # => false
+ * [0, 1, 2].include?(2) # => true
+ * [0, 1, 2].include?(2.0) # => true
+ * [0, 1, 2].include?(2.1) # => false
+ *
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
VALUE
@@ -5256,33 +5546,40 @@ recursive_cmp(VALUE ary1, VALUE ary2, int recur)
/*
* call-seq:
- * array <=> other_array -> -1, 0, or 1
- *
- * Returns -1, 0, or 1 as +self+ is less than, equal to, or greater than +other_array+.
- * For each index +i+ in +self+, evaluates <tt>result = self[i] <=> other_array[i]</tt>.
+ * self <=> other_array -> -1, 0, or 1
*
- * Returns -1 if any result is -1:
+ * Returns -1, 0, or 1 as +self+ is determined
+ * to be less than, equal to, or greater than +other_array+.
*
- * [0, 1, 2] <=> [0, 1, 3] # => -1
+ * Iterates over each index +i+ in <tt>(0...self.size)</tt>:
*
- * Returns 1 if any result is 1:
+ * - Computes <tt>result[i]</tt> as <tt>self[i] <=> other_array[i]</tt>.
+ * - Immediately returns 1 if <tt>result[i]</tt> is 1:
*
- * [0, 1, 2] <=> [0, 1, 1] # => 1
+ * [0, 1, 2] <=> [0, 0, 2] # => 1
*
- * When all results are zero:
+ * - Immediately returns -1 if <tt>result[i]</tt> is -1:
*
- * - Returns -1 if +array+ is smaller than +other_array+:
+ * [0, 1, 2] <=> [0, 2, 2] # => -1
*
- * [0, 1, 2] <=> [0, 1, 2, 3] # => -1
+ * - Continues if <tt>result[i]</tt> is 0.
*
- * - Returns 1 if +array+ is larger than +other_array+:
+ * When every +result+ is 0,
+ * returns <tt>self.size <=> other_array.size</tt>
+ * (see Integer#<=>):
*
- * [0, 1, 2] <=> [0, 1] # => 1
+ * [0, 1, 2] <=> [0, 1] # => 1
+ * [0, 1, 2] <=> [0, 1, 2] # => 0
+ * [0, 1, 2] <=> [0, 1, 2, 3] # => -1
*
- * - Returns 0 if +array+ and +other_array+ are the same size:
+ * Note that when +other_array+ is larger than +self+,
+ * its trailing elements do not affect the result:
*
- * [0, 1, 2] <=> [0, 1, 2] # => 0
+ * [0, 1, 2] <=> [0, 1, 2, -3] # => -1
+ * [0, 1, 2] <=> [0, 1, 2, 0] # => -1
+ * [0, 1, 2] <=> [0, 1, 2, 3] # => -1
*
+ * Related: see {Methods for Comparing}[rdoc-ref:Array@Methods+for+Comparing].
*/
VALUE
@@ -5352,18 +5649,20 @@ ary_make_hash_by(VALUE ary)
/*
* call-seq:
- * array - other_array -> new_array
+ * self - other_array -> new_array
*
- * Returns a new \Array containing only those elements from +array+
- * that are not found in \Array +other_array+;
- * items are compared using <tt>eql?</tt>;
- * the order from +array+ is preserved:
+ * Returns a new array containing only those elements of +self+
+ * that are not found in +other_array+;
+ * the order from +self+ is preserved:
*
- * [0, 1, 1, 2, 1, 1, 3, 1, 1] - [1] # => [0, 2, 3]
- * [0, 1, 2, 3] - [3, 0] # => [1, 2]
- * [0, 1, 2] - [4] # => [0, 1, 2]
+ * [0, 1, 1, 2, 1, 1, 3, 1, 1] - [1] # => [0, 2, 3]
+ * [0, 1, 1, 2, 1, 1, 3, 1, 1] - [3, 2, 0, :foo] # => [1, 1, 1, 1, 1, 1]
+ * [0, 1, 2] - [:foo] # => [0, 1, 2]
*
- * Related: Array#difference.
+ * Element are compared using method <tt>#eql?</tt>
+ * (as defined in each element of +self+).
+ *
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
VALUE
@@ -5397,19 +5696,21 @@ rb_ary_diff(VALUE ary1, VALUE ary2)
/*
* call-seq:
- * array.difference(*other_arrays) -> new_array
+ * difference(*other_arrays = []) -> new_array
*
- * Returns a new \Array containing only those elements from +self+
- * that are not found in any of the Arrays +other_arrays+;
+ * Returns a new array containing only those elements from +self+
+ * that are not found in any of the given +other_arrays+;
* items are compared using <tt>eql?</tt>; order from +self+ is preserved:
*
* [0, 1, 1, 2, 1, 1, 3, 1, 1].difference([1]) # => [0, 2, 3]
- * [0, 1, 2, 3].difference([3, 0], [1, 3]) # => [2]
- * [0, 1, 2].difference([4]) # => [0, 1, 2]
+ * [0, 1, 2, 3].difference([3, 0], [1, 3]) # => [2]
+ * [0, 1, 2].difference([4]) # => [0, 1, 2]
+ * [0, 1, 2].difference # => [0, 1, 2]
*
- * Returns a copy of +self+ if no arguments given.
+ * Returns a copy of +self+ if no arguments are given.
*
- * Related: Array#-.
+ * Related: Array#-;
+ * see also {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
static VALUE
@@ -5433,7 +5734,7 @@ rb_ary_difference_multi(int argc, VALUE *argv, VALUE ary)
VALUE elt = rb_ary_elt(ary, i);
for (j = 0; j < argc; j++) {
if (is_hash[j]) {
- if (rb_hash_stlike_lookup(argv[j], RARRAY_AREF(ary, i), NULL))
+ if (rb_hash_stlike_lookup(argv[j], elt, NULL))
break;
}
else {
@@ -5451,20 +5752,25 @@ rb_ary_difference_multi(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array & other_array -> new_array
+ * self & other_array -> new_array
*
- * Returns a new \Array containing each element found in both +array+ and \Array +other_array+;
- * duplicates are omitted; items are compared using <tt>eql?</tt>
- * (items must also implement +hash+ correctly):
+ * Returns a new array containing the _intersection_ of +self+ and +other_array+;
+ * that is, containing those elements found in both +self+ and +other_array+:
*
* [0, 1, 2, 3] & [1, 2] # => [1, 2]
- * [0, 1, 0, 1] & [0, 1] # => [0, 1]
*
- * Preserves order from +array+:
+ * Omits duplicates:
+ *
+ * [0, 1, 1, 0] & [0, 1] # => [0, 1]
+ *
+ * Preserves order from +self+:
*
* [0, 1, 2] & [3, 2, 1, 0] # => [0, 1, 2]
*
- * Related: Array#intersection.
+ * Identifies common elements using method <tt>#eql?</tt>
+ * (as defined in each element of +self+).
+ *
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
@@ -5504,23 +5810,23 @@ rb_ary_and(VALUE ary1, VALUE ary2)
/*
* call-seq:
- * array.intersection(*other_arrays) -> new_array
+ * intersection(*other_arrays) -> new_array
*
- * Returns a new \Array containing each element found both in +self+
- * and in all of the given Arrays +other_arrays+;
- * duplicates are omitted; items are compared using <tt>eql?</tt>
- * (items must also implement +hash+ correctly):
+ * Returns a new array containing each element in +self+ that is +#eql?+
+ * to at least one element in each of the given +other_arrays+;
+ * duplicates are omitted:
*
- * [0, 1, 2, 3].intersection([0, 1, 2], [0, 1, 3]) # => [0, 1]
* [0, 0, 1, 1, 2, 3].intersection([0, 1, 2], [0, 1, 3]) # => [0, 1]
*
- * Preserves order from +self+:
+ * Each element must correctly implement method <tt>#hash</tt>.
+ *
+ * Order from +self+ is preserved:
*
* [0, 1, 2].intersection([2, 1, 0]) # => [0, 1, 2]
*
- * Returns a copy of +self+ if no arguments given.
+ * Returns a copy of +self+ if no arguments are given.
*
- * Related: Array#&.
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
static VALUE
@@ -5569,9 +5875,9 @@ rb_ary_union_hash(VALUE hash, VALUE ary2)
/*
* call-seq:
- * array | other_array -> new_array
+ * self | other_array -> new_array
*
- * Returns the union of +array+ and \Array +other_array+;
+ * Returns the union of +self+ and +other_array+;
* duplicates are removed; order is preserved;
* items are compared using <tt>eql?</tt>:
*
@@ -5579,7 +5885,7 @@ rb_ary_union_hash(VALUE hash, VALUE ary2)
* [0, 1, 1] | [2, 2, 3] # => [0, 1, 2, 3]
* [0, 1, 2] | [3, 2, 1, 0] # => [0, 1, 2, 3]
*
- * Related: Array#union.
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
static VALUE
@@ -5603,18 +5909,25 @@ rb_ary_or(VALUE ary1, VALUE ary2)
/*
* call-seq:
- * array.union(*other_arrays) -> new_array
+ * union(*other_arrays) -> new_array
*
- * Returns a new \Array that is the union of +self+ and all given Arrays +other_arrays+;
- * duplicates are removed; order is preserved; items are compared using <tt>eql?</tt>:
+ * Returns a new array that is the union of the elements of +self+
+ * and all given arrays +other_arrays+;
+ * items are compared using <tt>eql?</tt>:
*
* [0, 1, 2, 3].union([4, 5], [6, 7]) # => [0, 1, 2, 3, 4, 5, 6, 7]
+ *
+ * Removes duplicates (preserving the first found):
+ *
* [0, 1, 1].union([2, 1], [3, 1]) # => [0, 1, 2, 3]
- * [0, 1, 2, 3].union([3, 2], [1, 0]) # => [0, 1, 2, 3]
*
- * Returns a copy of +self+ if no arguments given.
+ * Preserves order (preserving the position of the first found):
*
- * Related: Array#|.
+ * [3, 2, 1, 0].union([5, 3], [4, 2]) # => [3, 2, 1, 0, 5, 4]
+ *
+ * With no arguments given, returns a copy of +self+.
+ *
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
static VALUE
@@ -5647,19 +5960,16 @@ rb_ary_union_multi(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * ary.intersect?(other_ary) -> true or false
+ * intersect?(other_array) -> true or false
+ *
+ * Returns whether +other_array+ has at least one element that is +#eql?+ to some element of +self+:
*
- * Returns +true+ if the array and +other_ary+ have at least one element in
- * common, otherwise returns +false+:
+ * [1, 2, 3].intersect?([3, 4, 5]) # => true
+ * [1, 2, 3].intersect?([4, 5, 6]) # => false
*
- * a = [ 1, 2, 3 ]
- * b = [ 3, 4, 5 ]
- * c = [ 5, 6, 7 ]
- * a.intersect?(b) #=> true
- * a.intersect?(c) #=> false
+ * Each element must correctly implement method <tt>#hash</tt>.
*
- * Array elements are compared using <tt>eql?</tt>
- * (items must also implement +hash+ correctly).
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -5793,42 +6103,51 @@ ary_max_opt_string(VALUE ary, long i, VALUE vmax)
/*
* call-seq:
- * array.max -> element
- * array.max {|a, b| ... } -> element
- * array.max(n) -> new_array
- * array.max(n) {|a, b| ... } -> new_array
+ * max -> element
+ * max(count) -> new_array
+ * max {|a, b| ... } -> element
+ * max(count) {|a, b| ... } -> new_array
*
* Returns one of the following:
*
* - The maximum-valued element from +self+.
- * - A new \Array of maximum-valued elements selected from +self+.
+ * - A new array of maximum-valued elements from +self+.
*
- * When no block is given, each element in +self+ must respond to method <tt><=></tt>
- * with an \Integer.
+ * Does not modify +self+.
+ *
+ * With no block given, each element in +self+ must respond to method <tt>#<=></tt>
+ * with a numeric.
*
* With no argument and no block, returns the element in +self+
- * having the maximum value per method <tt><=></tt>:
+ * having the maximum value per method <tt>#<=></tt>:
*
- * [0, 1, 2].max # => 2
+ * [1, 0, 3, 2].max # => 3
*
- * With an argument \Integer +n+ and no block, returns a new \Array with at most +n+ elements,
- * in descending order per method <tt><=></tt>:
+ * With non-negative numeric argument +count+ and no block,
+ * returns a new array with at most +count+ elements,
+ * in descending order, per method <tt>#<=></tt>:
*
- * [0, 1, 2, 3].max(3) # => [3, 2, 1]
- * [0, 1, 2, 3].max(6) # => [3, 2, 1, 0]
+ * [1, 0, 3, 2].max(3) # => [3, 2, 1]
+ * [1, 0, 3, 2].max(3.0) # => [3, 2, 1]
+ * [1, 0, 3, 2].max(9) # => [3, 2, 1, 0]
+ * [1, 0, 3, 2].max(0) # => []
*
- * When a block is given, the block must return an \Integer.
+ * With a block given, the block must return a numeric.
*
- * With a block and no argument, calls the block <tt>self.size-1</tt> times to compare elements;
+ * With a block and no argument, calls the block <tt>self.size - 1</tt> times to compare elements;
* returns the element having the maximum value per the block:
*
- * ['0', '00', '000'].max {|a, b| a.size <=> b.size } # => "000"
+ * ['0', '', '000', '00'].max {|a, b| a.size <=> b.size }
+ * # => "000"
*
- * With an argument +n+ and a block, returns a new \Array with at most +n+ elements,
- * in descending order per the block:
+ * With non-negative numeric argument +count+ and a block,
+ * returns a new array with at most +count+ elements,
+ * in descending order, per the block:
*
- * ['0', '00', '000'].max(2) {|a, b| a.size <=> b.size } # => ["000", "00"]
+ * ['0', '', '000', '00'].max(2) {|a, b| a.size <=> b.size }
+ * # => ["000", "00"]
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
rb_ary_max(int argc, VALUE *argv, VALUE ary)
@@ -5961,42 +6280,51 @@ ary_min_opt_string(VALUE ary, long i, VALUE vmin)
/*
* call-seq:
- * array.min -> element
- * array.min { |a, b| ... } -> element
- * array.min(n) -> new_array
- * array.min(n) { |a, b| ... } -> new_array
+ * min -> element
+ * min(count) -> new_array
+ * min {|a, b| ... } -> element
+ * min(count) {|a, b| ... } -> new_array
*
* Returns one of the following:
*
* - The minimum-valued element from +self+.
- * - A new \Array of minimum-valued elements selected from +self+.
+ * - A new array of minimum-valued elements from +self+.
+ *
+ * Does not modify +self+.
*
- * When no block is given, each element in +self+ must respond to method <tt><=></tt>
- * with an \Integer.
+ * With no block given, each element in +self+ must respond to method <tt>#<=></tt>
+ * with a numeric.
*
* With no argument and no block, returns the element in +self+
- * having the minimum value per method <tt><=></tt>:
+ * having the minimum value per method <tt>#<=></tt>:
*
- * [0, 1, 2].min # => 0
+ * [1, 0, 3, 2].min # => 0
*
- * With \Integer argument +n+ and no block, returns a new \Array with at most +n+ elements,
- * in ascending order per method <tt><=></tt>:
+ * With non-negative numeric argument +count+ and no block,
+ * returns a new array with at most +count+ elements,
+ * in ascending order, per method <tt>#<=></tt>:
*
- * [0, 1, 2, 3].min(3) # => [0, 1, 2]
- * [0, 1, 2, 3].min(6) # => [0, 1, 2, 3]
+ * [1, 0, 3, 2].min(3) # => [0, 1, 2]
+ * [1, 0, 3, 2].min(3.0) # => [0, 1, 2]
+ * [1, 0, 3, 2].min(9) # => [0, 1, 2, 3]
+ * [1, 0, 3, 2].min(0) # => []
*
- * When a block is given, the block must return an Integer.
+ * With a block given, the block must return a numeric.
*
- * With a block and no argument, calls the block <tt>self.size-1</tt> times to compare elements;
+ * With a block and no argument, calls the block <tt>self.size - 1</tt> times to compare elements;
* returns the element having the minimum value per the block:
*
- * ['0', '00', '000'].min { |a, b| a.size <=> b.size } # => "0"
+ * ['0', '', '000', '00'].min {|a, b| a.size <=> b.size }
+ * # => ""
*
- * With an argument +n+ and a block, returns a new \Array with at most +n+ elements,
- * in ascending order per the block:
+ * With non-negative numeric argument +count+ and a block,
+ * returns a new array with at most +count+ elements,
+ * in ascending order, per the block:
*
- * ['0', '00', '000'].min(2) {|a, b| a.size <=> b.size } # => ["0", "00"]
+ * ['0', '', '000', '00'].min(2) {|a, b| a.size <=> b.size }
+ * # => ["", "0"]
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
rb_ary_min(int argc, VALUE *argv, VALUE ary)
@@ -6040,26 +6368,25 @@ rb_ary_min(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.minmax -> [min_val, max_val]
- * array.minmax {|a, b| ... } -> [min_val, max_val]
+ * minmax -> array
+ * minmax {|a, b| ... } -> array
*
- * Returns a new 2-element \Array containing the minimum and maximum values
- * from +self+, either per method <tt><=></tt> or per a given block:.
+ * Returns a 2-element array containing the minimum-valued and maximum-valued
+ * elements from +self+;
+ * does not modify +self+.
*
- * When no block is given, each element in +self+ must respond to method <tt><=></tt>
- * with an \Integer;
- * returns a new 2-element \Array containing the minimum and maximum values
- * from +self+, per method <tt><=></tt>:
+ * With no block given, the minimum and maximum values are determined using method <tt>#<=></tt>:
*
- * [0, 1, 2].minmax # => [0, 2]
+ * [1, 0, 3, 2].minmax # => [0, 3]
*
- * When a block is given, the block must return an \Integer;
- * the block is called <tt>self.size-1</tt> times to compare elements;
- * returns a new 2-element \Array containing the minimum and maximum values
- * from +self+, per the block:
+ * With a block given, the block must return a numeric;
+ * the block is called <tt>self.size - 1</tt> times to compare elements;
+ * returns the elements having the minimum and maximum values per the block:
*
- * ['0', '00', '000'].minmax {|a, b| a.size <=> b.size } # => ["0", "000"]
+ * ['0', '', '000', '00'].minmax {|a, b| a.size <=> b.size }
+ * # => ["", "000"]
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
rb_ary_minmax(VALUE ary)
@@ -6079,32 +6406,30 @@ push_value(st_data_t key, st_data_t val, st_data_t ary)
/*
* call-seq:
- * array.uniq! -> self or nil
- * array.uniq! {|element| ... } -> self or nil
+ * uniq! -> self or nil
+ * uniq! {|element| ... } -> self or nil
*
* Removes duplicate elements from +self+, the first occurrence always being retained;
* returns +self+ if any elements removed, +nil+ otherwise.
*
* With no block given, identifies and removes elements using method <tt>eql?</tt>
- * to compare.
- *
- * Returns +self+ if any elements removed:
+ * to compare elements:
*
* a = [0, 0, 1, 1, 2, 2]
* a.uniq! # => [0, 1, 2]
- *
- * Returns +nil+ if no elements removed.
+ * a.uniq! # => nil
*
* With a block given, calls the block for each element;
- * identifies (using method <tt>eql?</tt>) and removes
- * elements for which the block returns duplicate values.
- *
- * Returns +self+ if any elements removed:
+ * identifies and omits "duplicate" elements using method <tt>eql?</tt>
+ * to compare <i>block return values</i>;
+ * that is, an element is a duplicate if its block return value
+ * is the same as that of a previous element:
*
* a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb']
- * a.uniq! {|element| element.size } # => ['a', 'aa', 'aaa']
+ * a.uniq! {|element| element.size } # => ["a", "aa", "aaa"]
+ * a.uniq! {|element| element.size } # => nil
*
- * Returns +nil+ if no elements removed.
+ * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
rb_ary_uniq_bang(VALUE ary)
@@ -6126,7 +6451,7 @@ rb_ary_uniq_bang(VALUE ary)
}
rb_ary_modify_check(ary);
ARY_SET_LEN(ary, 0);
- if (ARY_SHARED_P(ary) && !ARY_EMBED_P(ary)) {
+ if (ARY_SHARED_P(ary)) {
rb_ary_unshare(ary);
FL_SET_EMBED(ary);
}
@@ -6138,25 +6463,28 @@ rb_ary_uniq_bang(VALUE ary)
/*
* call-seq:
- * array.uniq -> new_array
- * array.uniq {|element| ... } -> new_array
+ * uniq -> new_array
+ * uniq {|element| ... } -> new_array
*
- * Returns a new \Array containing those elements from +self+ that are not duplicates,
+ * Returns a new array containing those elements from +self+ that are not duplicates,
* the first occurrence always being retained.
*
- * With no block given, identifies and omits duplicates using method <tt>eql?</tt>
- * to compare:
+ * With no block given, identifies and omits duplicate elements using method <tt>eql?</tt>
+ * to compare elements:
*
* a = [0, 0, 1, 1, 2, 2]
* a.uniq # => [0, 1, 2]
*
* With a block given, calls the block for each element;
- * identifies (using method <tt>eql?</tt>) and omits duplicate values,
- * that is, those elements for which the block returns the same value:
+ * identifies and omits "duplicate" elements using method <tt>eql?</tt>
+ * to compare <i>block return values</i>;
+ * that is, an element is a duplicate if its block return value
+ * is the same as that of a previous element:
*
* a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb']
* a.uniq {|element| element.size } # => ["a", "aa", "aaa"]
*
+ * Related: {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -6182,11 +6510,18 @@ rb_ary_uniq(VALUE ary)
/*
* call-seq:
- * array.compact! -> self or nil
+ * compact! -> self or nil
*
- * Removes all +nil+ elements from +self+.
+ * Removes all +nil+ elements from +self+;
+ * Returns +self+ if any elements are removed, +nil+ otherwise:
*
- * Returns +self+ if any elements removed, otherwise +nil+.
+ * a = [nil, 0, nil, false, nil, '', nil, [], nil, {}]
+ * a.compact! # => [0, false, "", [], {}]
+ * a # => [0, false, "", [], {}]
+ * a.compact! # => nil
+ *
+ * Related: Array#compact;
+ * see also {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
@@ -6214,12 +6549,16 @@ rb_ary_compact_bang(VALUE ary)
/*
* call-seq:
- * array.compact -> new_array
+ * compact -> new_array
+ *
+ * Returns a new array containing only the non-+nil+ elements from +self+;
+ * element order is preserved:
*
- * Returns a new \Array containing all non-+nil+ elements from +self+:
+ * a = [nil, 0, nil, false, nil, '', nil, [], nil, {}]
+ * a.compact # => [0, false, "", [], {}]
*
- * a = [nil, 0, nil, 1, nil, 2, nil]
- * a.compact # => [0, 1, 2]
+ * Related: Array#compact!;
+ * see also {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting].
*/
static VALUE
@@ -6232,29 +6571,29 @@ rb_ary_compact(VALUE ary)
/*
* call-seq:
- * array.count -> an_integer
- * array.count(obj) -> an_integer
- * array.count {|element| ... } -> an_integer
+ * count -> integer
+ * count(object) -> integer
+ * count {|element| ... } -> integer
*
* Returns a count of specified elements.
*
* With no argument and no block, returns the count of all elements:
*
- * [0, 1, 2].count # => 3
- * [].count # => 0
+ * [0, :one, 'two', 3, 3.0].count # => 5
*
- * With argument +obj+, returns the count of elements <tt>==</tt> to +obj+:
+ * With argument +object+ given, returns the count of elements <tt>==</tt> to +object+:
*
- * [0, 1, 2, 0.0].count(0) # => 2
- * [0, 1, 2].count(3) # => 0
+ * [0, :one, 'two', 3, 3.0].count(3) # => 2
*
* With no argument and a block given, calls the block with each element;
* returns the count of elements for which the block returns a truthy value:
*
- * [0, 1, 2, 3].count {|element| element > 1} # => 2
+ * [0, 1, 2, 3].count {|element| element > 1 } # => 2
*
- * With argument +obj+ and a block given, issues a warning, ignores the block,
- * and returns the count of elements <tt>==</tt> to +obj+.
+ * With argument +object+ and a block given, issues a warning, ignores the block,
+ * and returns the count of elements <tt>==</tt> to +object+.
+ *
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -6290,16 +6629,9 @@ rb_ary_count(int argc, VALUE *argv, VALUE ary)
static VALUE
flatten(VALUE ary, int level)
{
- static const rb_data_type_t flatten_memo_data_type = {
- .wrap_struct_name = "array_flatten_memo_data_type",
- .function = { NULL, (RUBY_DATA_FUNC)st_free_table },
- NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
- };
-
long i;
- VALUE stack, result, tmp = 0, elt, vmemo;
- st_table *memo = 0;
- st_data_t id;
+ VALUE stack, result, tmp = 0, elt;
+ VALUE memo = Qfalse;
for (i = 0; i < RARRAY_LEN(ary); i++) {
elt = RARRAY_AREF(ary, i);
@@ -6321,10 +6653,9 @@ flatten(VALUE ary, int level)
rb_ary_push(stack, LONG2NUM(i + 1));
if (level < 0) {
- memo = st_init_numtable();
- vmemo = TypedData_Wrap_Struct(0, &flatten_memo_data_type, memo);
- st_insert(memo, (st_data_t)ary, (st_data_t)Qtrue);
- st_insert(memo, (st_data_t)tmp, (st_data_t)Qtrue);
+ memo = rb_obj_hide(rb_ident_hash_new());
+ rb_hash_aset(memo, ary, Qtrue);
+ rb_hash_aset(memo, tmp, Qtrue);
}
ary = tmp;
@@ -6339,9 +6670,8 @@ flatten(VALUE ary, int level)
}
tmp = rb_check_array_type(elt);
if (RBASIC(result)->klass) {
- if (memo) {
- RB_GC_GUARD(vmemo);
- st_clear(memo);
+ if (RTEST(memo)) {
+ rb_hash_clear(memo);
}
rb_raise(rb_eRuntimeError, "flatten reentered");
}
@@ -6350,12 +6680,11 @@ flatten(VALUE ary, int level)
}
else {
if (memo) {
- id = (st_data_t)tmp;
- if (st_is_member(memo, id)) {
- st_clear(memo);
+ if (rb_hash_aref(memo, tmp) == Qtrue) {
+ rb_hash_clear(memo);
rb_raise(rb_eArgError, "tried to flatten recursive array");
}
- st_insert(memo, id, (st_data_t)Qtrue);
+ rb_hash_aset(memo, tmp, Qtrue);
}
rb_ary_push(stack, ary);
rb_ary_push(stack, LONG2NUM(i));
@@ -6367,8 +6696,7 @@ flatten(VALUE ary, int level)
break;
}
if (memo) {
- id = (st_data_t)ary;
- st_delete(memo, &id, 0);
+ rb_hash_delete(memo, ary);
}
tmp = rb_ary_pop(stack);
i = NUM2LONG(tmp);
@@ -6376,7 +6704,7 @@ flatten(VALUE ary, int level)
}
if (memo) {
- st_clear(memo);
+ rb_hash_clear(memo);
}
RBASIC_SET_CLASS(result, rb_cArray);
@@ -6385,33 +6713,37 @@ flatten(VALUE ary, int level)
/*
* call-seq:
- * array.flatten! -> self or nil
- * array.flatten!(level) -> self or nil
+ * flatten!(depth = nil) -> self or nil
+ *
+ * Returns +self+ as a recursively flattening of +self+ to +depth+ levels of recursion;
+ * +depth+ must be an
+ * {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects],
+ * or +nil+.
+ * At each level of recursion:
+ *
+ * - Each element that is an array is "flattened"
+ * (that is, replaced by its individual array elements).
+ * - Each element that is not an array is unchanged
+ * (even if the element is an object that has instance method +flatten+).
*
- * Replaces each nested \Array in +self+ with the elements from that \Array;
- * returns +self+ if any changes, +nil+ otherwise.
+ * Returns +nil+ if no elements were flattened.
*
- * With non-negative \Integer argument +level+, flattens recursively through +level+ levels:
+ * With non-negative integer argument +depth+, flattens recursively through +depth+ levels:
*
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten!(1) # => [0, 1, [2, 3], 4, 5]
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten!(2) # => [0, 1, 2, 3, 4, 5]
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten!(3) # => [0, 1, 2, 3, 4, 5]
- * [0, 1, 2].flatten!(1) # => nil
+ * a = [ 0, [ 1, [2, 3], 4 ], 5, {foo: 0}, Set.new([6, 7]) ]
+ * a # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.dup.flatten!(1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.dup.flatten!(1.1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.dup.flatten!(2) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.dup.flatten!(3) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
*
- * With no argument, a +nil+ argument, or with negative argument +level+, flattens all levels:
+ * With +nil+ or negative argument +depth+, flattens all levels:
*
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten! # => [0, 1, 2, 3, 4, 5]
- * [0, 1, 2].flatten! # => nil
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten!(-1) # => [0, 1, 2, 3, 4, 5]
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten!(-2) # => [0, 1, 2, 3, 4, 5]
- * [0, 1, 2].flatten!(-1) # => nil
+ * a.dup.flatten! # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.dup.flatten!(-1) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
*
+ * Related: Array#flatten;
+ * see also {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/
static VALUE
@@ -6429,7 +6761,7 @@ rb_ary_flatten_bang(int argc, VALUE *argv, VALUE ary)
if (result == ary) {
return Qnil;
}
- if (!(mod = ARY_EMBED_P(result))) rb_obj_freeze(result);
+ if (!(mod = ARY_EMBED_P(result))) rb_ary_freeze(result);
rb_ary_replace(ary, result);
if (mod) ARY_SET_EMBED_LEN(result, 0);
@@ -6438,35 +6770,37 @@ rb_ary_flatten_bang(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.flatten -> new_array
- * array.flatten(level) -> new_array
- *
- * Returns a new \Array that is a recursive flattening of +self+:
- * - Each non-Array element is unchanged.
- * - Each \Array is replaced by its individual elements.
- *
- * With non-negative \Integer argument +level+, flattens recursively through +level+ levels:
- *
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten(0) # => [0, [1, [2, 3], 4], 5]
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten(1) # => [0, 1, [2, 3], 4, 5]
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten(2) # => [0, 1, 2, 3, 4, 5]
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten(3) # => [0, 1, 2, 3, 4, 5]
- *
- * With no argument, a +nil+ argument, or with negative argument +level+, flattens all levels:
- *
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten # => [0, 1, 2, 3, 4, 5]
- * [0, 1, 2].flatten # => [0, 1, 2]
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten(-1) # => [0, 1, 2, 3, 4, 5]
- * a = [ 0, [ 1, [2, 3], 4 ], 5 ]
- * a.flatten(-2) # => [0, 1, 2, 3, 4, 5]
- * [0, 1, 2].flatten(-1) # => [0, 1, 2]
+ * flatten(depth = nil) -> new_array
+ *
+ * Returns a new array that is a recursive flattening of +self+
+ * to +depth+ levels of recursion;
+ * +depth+ must be an
+ * {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]
+ * or +nil+.
+ * At each level of recursion:
+ *
+ * - Each element that is an array is "flattened"
+ * (that is, replaced by its individual array elements).
+ * - Each element that is not an array is unchanged
+ * (even if the element is an object that has instance method +flatten+).
+ *
+ * With non-negative integer argument +depth+, flattens recursively through +depth+ levels:
+ *
+ * a = [ 0, [ 1, [2, 3], 4 ], 5, {foo: 0}, Set.new([6, 7]) ]
+ * a # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.flatten(0) # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.flatten(1 ) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.flatten(1.1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.flatten(2) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.flatten(3) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
+ *
+ * With +nil+ or negative +depth+, flattens all levels.
+ *
+ * a.flatten # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
+ * a.flatten(-1) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
*
+ * Related: Array#flatten!;
+ * see also {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/
static VALUE
@@ -6498,7 +6832,7 @@ rb_ary_shuffle_bang(rb_execution_context_t *ec, VALUE ary, VALUE randgen)
rb_ary_modify(ary);
i = len = RARRAY_LEN(ary);
RARRAY_PTR_USE(ary, ptr, {
- while (i) {
+ while (i > 1) {
long j = RAND_UPTO(i);
VALUE tmp;
if (len != RARRAY_LEN(ary) || ptr != RARRAY_CONST_PTR(ary)) {
@@ -6520,6 +6854,14 @@ rb_ary_shuffle(rb_execution_context_t *ec, VALUE ary, VALUE randgen)
return ary;
}
+static const rb_data_type_t ary_sample_memo_type = {
+ .wrap_struct_name = "ary_sample_memo",
+ .function = {
+ .dfree = (RUBY_DATA_FUNC)st_free_table,
+ },
+ .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
+};
+
static VALUE
ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE to_array)
{
@@ -6601,11 +6943,9 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE
}
else if (n <= memo_threshold / 2) {
long max_idx = 0;
-#undef RUBY_UNTYPED_DATA_WARNING
-#define RUBY_UNTYPED_DATA_WARNING 0
- VALUE vmemo = Data_Wrap_Struct(0, 0, st_free_table, 0);
+ VALUE vmemo = TypedData_Wrap_Struct(0, &ary_sample_memo_type, 0);
st_table *memo = st_init_numtable_with_size(n);
- DATA_PTR(vmemo) = memo;
+ RTYPEDDATA_DATA(vmemo) = memo;
result = rb_ary_new_capa(n);
RARRAY_PTR_USE(result, ptr_result, {
for (i=0; i<n; i++) {
@@ -6628,8 +6968,9 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE
}
});
});
- DATA_PTR(vmemo) = 0;
+ RTYPEDDATA_DATA(vmemo) = 0;
st_free_table(memo);
+ RB_GC_GUARD(vmemo);
}
else {
result = rb_ary_dup(ary);
@@ -6651,6 +6992,12 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE
}
static VALUE
+ary_sized_alloc(rb_execution_context_t *ec, VALUE self)
+{
+ return rb_ary_new2(RARRAY_LEN(self));
+}
+
+static VALUE
ary_sample0(rb_execution_context_t *ec, VALUE ary)
{
return ary_sample(ec, ary, rb_cRandom, Qfalse, Qfalse);
@@ -6674,36 +7021,36 @@ rb_ary_cycle_size(VALUE self, VALUE args, VALUE eobj)
/*
* call-seq:
- * array.cycle {|element| ... } -> nil
- * array.cycle(count) {|element| ... } -> nil
- * array.cycle -> new_enumerator
- * array.cycle(count) -> new_enumerator
+ * cycle(count = nil) {|element| ... } -> nil
+ * cycle(count = nil) -> new_enumerator
+ *
+ * With a block given, may call the block, depending on the value of argument +count+;
+ * +count+ must be an
+ * {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects],
+ * or +nil+.
*
- * When called with positive \Integer argument +count+ and a block,
- * calls the block with each element, then does so again,
+ * When +count+ is positive,
+ * calls the block with each element, then does so repeatedly,
* until it has done so +count+ times; returns +nil+:
*
* output = []
* [0, 1].cycle(2) {|element| output.push(element) } # => nil
* output # => [0, 1, 0, 1]
*
- * If +count+ is zero or negative, does not call the block:
+ * When +count+ is zero or negative, does not call the block:
*
- * [0, 1].cycle(0) {|element| fail 'Cannot happen' } # => nil
+ * [0, 1].cycle(0) {|element| fail 'Cannot happen' } # => nil
* [0, 1].cycle(-1) {|element| fail 'Cannot happen' } # => nil
*
- * When a block is given, and argument is omitted or +nil+, cycles forever:
+ * When +count+ is +nil+, cycles forever:
*
* # Prints 0 and 1 forever.
* [0, 1].cycle {|element| puts element }
* [0, 1].cycle(nil) {|element| puts element }
*
- * When no block is given, returns a new \Enumerator:
- *
- * [0, 1].cycle(2) # => #<Enumerator: [0, 1]:cycle(2)>
- * [0, 1].cycle # => # => #<Enumerator: [0, 1]:cycle>
- * [0, 1].cycle.first(5) # => [0, 1, 0, 1, 0]
+ * With no block given, returns a new Enumerator.
*
+ * Related: see {Methods for Iterating}[rdoc-ref:Array@Methods+for+Iterating].
*/
static VALUE
rb_ary_cycle(int argc, VALUE *argv, VALUE ary)
@@ -6847,82 +7194,44 @@ rb_ary_permutation_size(VALUE ary, VALUE args, VALUE eobj)
/*
* call-seq:
- * array.permutation {|element| ... } -> self
- * array.permutation(n) {|element| ... } -> self
- * array.permutation -> new_enumerator
- * array.permutation(n) -> new_enumerator
+ * permutation(count = self.size) {|permutation| ... } -> self
+ * permutation(count = self.size) -> new_enumerator
*
- * When invoked with a block, yield all permutations of elements of +self+; returns +self+.
- * The order of permutations is indeterminate.
+ * Iterates over permutations of the elements of +self+;
+ * the order of permutations is indeterminate.
*
- * When a block and an in-range positive \Integer argument +n+ (<tt>0 < n <= self.size</tt>)
- * are given, calls the block with all +n+-tuple permutations of +self+.
- *
- * Example:
- *
- * a = [0, 1, 2]
- * a.permutation(2) {|permutation| p permutation }
- *
- * Output:
- *
- * [0, 1]
- * [0, 2]
- * [1, 0]
- * [1, 2]
- * [2, 0]
- * [2, 1]
- *
- * Another example:
+ * With a block and an in-range positive integer argument +count+ (<tt>0 < count <= self.size</tt>) given,
+ * calls the block with each permutation of +self+ of size +count+;
+ * returns +self+:
*
* a = [0, 1, 2]
- * a.permutation(3) {|permutation| p permutation }
- *
- * Output:
+ * perms = []
+ * a.permutation(1) {|perm| perms.push(perm) }
+ * perms # => [[0], [1], [2]]
*
- * [0, 1, 2]
- * [0, 2, 1]
- * [1, 0, 2]
- * [1, 2, 0]
- * [2, 0, 1]
- * [2, 1, 0]
+ * perms = []
+ * a.permutation(2) {|perm| perms.push(perm) }
+ * perms # => [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]
*
- * When +n+ is zero, calls the block once with a new empty \Array:
+ * perms = []
+ * a.permutation(3) {|perm| perms.push(perm) }
+ * perms # => [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
*
- * a = [0, 1, 2]
- * a.permutation(0) {|permutation| p permutation }
+ * When +count+ is zero, calls the block once with a new empty array:
*
- * Output:
+ * perms = []
+ * a.permutation(0) {|perm| perms.push(perm) }
+ * perms # => [[]]
*
- * []
- *
- * When +n+ is out of range (negative or larger than <tt>self.size</tt>),
+ * When +count+ is out of range (negative or larger than <tt>self.size</tt>),
* does not call the block:
*
- * a = [0, 1, 2]
* a.permutation(-1) {|permutation| fail 'Cannot happen' }
* a.permutation(4) {|permutation| fail 'Cannot happen' }
*
- * When a block given but no argument,
- * behaves the same as <tt>a.permutation(a.size)</tt>:
- *
- * a = [0, 1, 2]
- * a.permutation {|permutation| p permutation }
- *
- * Output:
- *
- * [0, 1, 2]
- * [0, 2, 1]
- * [1, 0, 2]
- * [1, 2, 0]
- * [2, 0, 1]
- * [2, 1, 0]
- *
- * Returns a new \Enumerator if no block given:
- *
- * a = [0, 1, 2]
- * a.permutation # => #<Enumerator: [0, 1, 2]:permutation>
- * a.permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
+ * With no block given, returns a new Enumerator.
*
+ * Related: {Methods for Iterating}[rdoc-ref:Array@Methods+for+Iterating].
*/
static VALUE
@@ -6995,56 +7304,46 @@ rb_ary_combination_size(VALUE ary, VALUE args, VALUE eobj)
/*
* call-seq:
- * array.combination(n) {|element| ... } -> self
- * array.combination(n) -> new_enumerator
- *
- * Calls the block, if given, with combinations of elements of +self+;
- * returns +self+. The order of combinations is indeterminate.
- *
- * When a block and an in-range positive \Integer argument +n+ (<tt>0 < n <= self.size</tt>)
- * are given, calls the block with all +n+-tuple combinations of +self+.
+ * combination(count) {|element| ... } -> self
+ * combination(count) -> new_enumerator
*
- * Example:
+ * When a block and a positive
+ * {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]
+ * argument +count+ (<tt>0 < count <= self.size</tt>)
+ * are given, calls the block with each combination of +self+ of size +count+;
+ * returns +self+:
*
- * a = [0, 1, 2]
- * a.combination(2) {|combination| p combination }
+ * a = %w[a b c] # => ["a", "b", "c"]
+ * a.combination(2) {|combination| p combination } # => ["a", "b", "c"]
*
* Output:
*
- * [0, 1]
- * [0, 2]
- * [1, 2]
- *
- * Another example:
+ * ["a", "b"]
+ * ["a", "c"]
+ * ["b", "c"]
*
- * a = [0, 1, 2]
- * a.combination(3) {|combination| p combination }
- *
- * Output:
+ * The order of the yielded combinations is not guaranteed.
*
- * [0, 1, 2]
+ * When +count+ is zero, calls the block once with a new empty array:
*
- * When +n+ is zero, calls the block once with a new empty \Array:
- *
- * a = [0, 1, 2]
- * a1 = a.combination(0) {|combination| p combination }
+ * a.combination(0) {|combination| p combination }
+ * [].combination(0) {|combination| p combination }
*
* Output:
*
* []
+ * []
*
- * When +n+ is out of range (negative or larger than <tt>self.size</tt>),
+ * When +count+ is negative or larger than +self.size+ and +self+ is non-empty,
* does not call the block:
*
- * a = [0, 1, 2]
- * a.combination(-1) {|combination| fail 'Cannot happen' }
- * a.combination(4) {|combination| fail 'Cannot happen' }
+ * a.combination(-1) {|combination| fail 'Cannot happen' } # => ["a", "b", "c"]
+ * a.combination(4) {|combination| fail 'Cannot happen' } # => ["a", "b", "c"]
*
- * Returns a new \Enumerator if no block given:
- *
- * a = [0, 1, 2]
- * a.combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>
+ * With no block given, returns a new Enumerator.
*
+ * Related: Array#permutation;
+ * see also {Methods for Iterating}[rdoc-ref:Array@Methods+for+Iterating].
*/
static VALUE
@@ -7131,68 +7430,41 @@ rb_ary_repeated_permutation_size(VALUE ary, VALUE args, VALUE eobj)
/*
* call-seq:
- * array.repeated_permutation(n) {|permutation| ... } -> self
- * array.repeated_permutation(n) -> new_enumerator
+ * repeated_permutation(size) {|permutation| ... } -> self
+ * repeated_permutation(size) -> new_enumerator
*
- * Calls the block with each repeated permutation of length +n+ of the elements of +self+;
- * each permutation is an \Array;
+ * With a block given, calls the block with each repeated permutation of length +size+
+ * of the elements of +self+;
+ * each permutation is an array;
* returns +self+. The order of the permutations is indeterminate.
*
- * When a block and a positive \Integer argument +n+ are given, calls the block with each
- * +n+-tuple repeated permutation of the elements of +self+.
- * The number of permutations is <tt>self.size**n</tt>.
- *
- * +n+ = 1:
+ * If a positive integer argument +size+ is given,
+ * calls the block with each +size+-tuple repeated permutation of the elements of +self+.
+ * The number of permutations is <tt>self.size**size</tt>.
*
- * a = [0, 1, 2]
- * a.repeated_permutation(1) {|permutation| p permutation }
- *
- * Output:
- *
- * [0]
- * [1]
- * [2]
- *
- * +n+ = 2:
- *
- * a.repeated_permutation(2) {|permutation| p permutation }
- *
- * Output:
+ * Examples:
*
- * [0, 0]
- * [0, 1]
- * [0, 2]
- * [1, 0]
- * [1, 1]
- * [1, 2]
- * [2, 0]
- * [2, 1]
- * [2, 2]
+ * - +size+ is 1:
*
- * If +n+ is zero, calls the block once with an empty \Array.
+ * p = []
+ * [0, 1, 2].repeated_permutation(1) {|permutation| p.push(permutation) }
+ * p # => [[0], [1], [2]]
*
- * If +n+ is negative, does not call the block:
+ * - +size+ is 2:
*
- * a.repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
+ * p = []
+ * [0, 1, 2].repeated_permutation(2) {|permutation| p.push(permutation) }
+ * p # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
*
- * Returns a new \Enumerator if no block given:
+ * If +size+ is zero, calls the block once with an empty array.
*
- * a = [0, 1, 2]
- * a.repeated_permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
+ * If +size+ is negative, does not call the block:
*
- * Using Enumerators, it's convenient to show the permutations and counts
- * for some values of +n+:
+ * [0, 1, 2].repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
*
- * e = a.repeated_permutation(0)
- * e.size # => 1
- * e.to_a # => [[]]
- * e = a.repeated_permutation(1)
- * e.size # => 3
- * e.to_a # => [[0], [1], [2]]
- * e = a.repeated_permutation(2)
- * e.size # => 9
- * e.to_a # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
+ * With no block given, returns a new Enumerator.
*
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
static VALUE
rb_ary_repeated_permutation(VALUE ary, VALUE num)
@@ -7263,65 +7535,41 @@ rb_ary_repeated_combination_size(VALUE ary, VALUE args, VALUE eobj)
/*
* call-seq:
- * array.repeated_combination(n) {|combination| ... } -> self
- * array.repeated_combination(n) -> new_enumerator
+ * repeated_combination(size) {|combination| ... } -> self
+ * repeated_combination(size) -> new_enumerator
*
- * Calls the block with each repeated combination of length +n+ of the elements of +self+;
- * each combination is an \Array;
+ * With a block given, calls the block with each repeated combination of length +size+
+ * of the elements of +self+;
+ * each combination is an array;
* returns +self+. The order of the combinations is indeterminate.
*
- * When a block and a positive \Integer argument +n+ are given, calls the block with each
- * +n+-tuple repeated combination of the elements of +self+.
- * The number of combinations is <tt>(n+1)(n+2)/2</tt>.
- *
- * +n+ = 1:
- *
- * a = [0, 1, 2]
- * a.repeated_combination(1) {|combination| p combination }
- *
- * Output:
- *
- * [0]
- * [1]
- * [2]
- *
- * +n+ = 2:
+ * If a positive integer argument +size+ is given,
+ * calls the block with each +size+-tuple repeated combination of the elements of +self+.
+ * The number of combinations is <tt>(size+1)(size+2)/2</tt>.
*
- * a.repeated_combination(2) {|combination| p combination }
- *
- * Output:
+ * Examples:
*
- * [0, 0]
- * [0, 1]
- * [0, 2]
- * [1, 1]
- * [1, 2]
- * [2, 2]
+ * - +size+ is 1:
*
- * If +n+ is zero, calls the block once with an empty \Array.
+ * c = []
+ * [0, 1, 2].repeated_combination(1) {|combination| c.push(combination) }
+ * c # => [[0], [1], [2]]
*
- * If +n+ is negative, does not call the block:
+ * - +size+ is 2:
*
- * a.repeated_combination(-1) {|combination| fail 'Cannot happen' }
+ * c = []
+ * [0, 1, 2].repeated_combination(2) {|combination| c.push(combination) }
+ * c # => [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]
*
- * Returns a new \Enumerator if no block given:
+ * If +size+ is zero, calls the block once with an empty array.
*
- * a = [0, 1, 2]
- * a.repeated_combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>
+ * If +size+ is negative, does not call the block:
*
- * Using Enumerators, it's convenient to show the combinations and counts
- * for some values of +n+:
+ * [0, 1, 2].repeated_combination(-1) {|combination| fail 'Cannot happen' }
*
- * e = a.repeated_combination(0)
- * e.size # => 1
- * e.to_a # => [[]]
- * e = a.repeated_combination(1)
- * e.size # => 3
- * e.to_a # => [[0], [1], [2]]
- * e = a.repeated_combination(2)
- * e.size # => 6
- * e.to_a # => [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]
+ * With no block given, returns a new Enumerator.
*
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
static VALUE
@@ -7361,62 +7609,55 @@ rb_ary_repeated_combination(VALUE ary, VALUE num)
/*
* call-seq:
- * array.product(*other_arrays) -> new_array
- * array.product(*other_arrays) {|combination| ... } -> self
+ * product(*other_arrays) -> new_array
+ * product(*other_arrays) {|combination| ... } -> self
*
- * Computes and returns or yields all combinations of elements from all the Arrays,
+ * Computes all combinations of elements from all the arrays,
* including both +self+ and +other_arrays+:
*
* - The number of combinations is the product of the sizes of all the arrays,
* including both +self+ and +other_arrays+.
* - The order of the returned combinations is indeterminate.
*
- * When no block is given, returns the combinations as an \Array of Arrays:
+ * With no block given, returns the combinations as an array of arrays:
*
- * a = [0, 1, 2]
- * a1 = [3, 4]
- * a2 = [5, 6]
- * p = a.product(a1)
- * p.size # => 6 # a.size * a1.size
- * p # => [[0, 3], [0, 4], [1, 3], [1, 4], [2, 3], [2, 4]]
- * p = a.product(a1, a2)
- * p.size # => 12 # a.size * a1.size * a2.size
- * p # => [[0, 3, 5], [0, 3, 6], [0, 4, 5], [0, 4, 6], [1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6]]
- *
- * If any argument is an empty \Array, returns an empty \Array.
- *
- * If no argument is given, returns an \Array of 1-element Arrays,
- * each containing an element of +self+:
+ * p = [0, 1].product([2, 3])
+ * # => [[0, 2], [0, 3], [1, 2], [1, 3]]
+ * p.size # => 4
+ * p = [0, 1].product([2, 3], [4, 5])
+ * # => [[0, 2, 4], [0, 2, 5], [0, 3, 4], [0, 3, 5], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3,...
+ * p.size # => 8
*
- * a.product # => [[0], [1], [2]]
+ * If +self+ or any argument is empty, returns an empty array:
*
- * When a block is given, yields each combination as an \Array; returns +self+:
+ * [].product([2, 3], [4, 5]) # => []
+ * [0, 1].product([2, 3], []) # => []
*
- * a.product(a1) {|combination| p combination }
+ * If no argument is given, returns an array of 1-element arrays,
+ * each containing an element of +self+:
*
- * Output:
+ * a.product # => [[0], [1], [2]]
*
- * [0, 3]
- * [0, 4]
- * [1, 3]
- * [1, 4]
- * [2, 3]
- * [2, 4]
+ * With a block given, calls the block with each combination; returns +self+:
*
- * If any argument is an empty \Array, does not call the block:
+ * p = []
+ * [0, 1].product([2, 3]) {|combination| p.push(combination) }
+ * p # => [[0, 2], [0, 3], [1, 2], [1, 3]]
*
- * a.product(a1, a2, []) {|combination| fail 'Cannot happen' }
+ * If +self+ or any argument is empty, does not call the block:
*
- * If no argument is given, yields each element of +self+ as a 1-element \Array:
+ * [].product([2, 3], [4, 5]) {|combination| fail 'Cannot happen' }
+ * # => []
+ * [0, 1].product([2, 3], []) {|combination| fail 'Cannot happen' }
+ * # => [0, 1]
*
- * a.product {|combination| p combination }
+ * If no argument is given, calls the block with each element of +self+ as a 1-element array:
*
- * Output:
- *
- * [0]
- * [1]
- * [2]
+ * p = []
+ * [0, 1].product {|combination| p.push(combination) }
+ * p # => [[0], [1]]
*
+ * Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
static VALUE
@@ -7509,20 +7750,20 @@ done:
/*
* call-seq:
- * array.take(n) -> new_array
+ * take(count) -> new_array
*
- * Returns a new \Array containing the first +n+ element of +self+,
- * where +n+ is a non-negative \Integer;
- * does not modify +self+.
+ * Returns a new array containing the first +count+ element of +self+
+ * (as available);
+ * +count+ must be a non-negative numeric;
+ * does not modify +self+:
*
- * Examples:
- *
- * a = [0, 1, 2, 3, 4, 5]
- * a.take(1) # => [0]
- * a.take(2) # => [0, 1]
- * a.take(50) # => [0, 1, 2, 3, 4, 5]
- * a # => [0, 1, 2, 3, 4, 5]
+ * a = ['a', 'b', 'c', 'd']
+ * a.take(2) # => ["a", "b"]
+ * a.take(2.1) # => ["a", "b"]
+ * a.take(50) # => ["a", "b", "c", "d"]
+ * a.take(0) # => []
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -7537,25 +7778,23 @@ rb_ary_take(VALUE obj, VALUE n)
/*
* call-seq:
- * array.take_while {|element| ... } -> new_array
- * array.take_while -> new_enumerator
- *
- * Returns a new \Array containing zero or more leading elements of +self+;
- * does not modify +self+.
+ * take_while {|element| ... } -> new_array
+ * take_while -> new_enumerator
*
* With a block given, calls the block with each successive element of +self+;
- * stops if the block returns +false+ or +nil+;
- * returns a new \Array containing those elements for which the block returned a truthy value:
+ * stops iterating if the block returns +false+ or +nil+;
+ * returns a new array containing those elements for which the block returned a truthy value:
*
* a = [0, 1, 2, 3, 4, 5]
* a.take_while {|element| element < 3 } # => [0, 1, 2]
- * a.take_while {|element| true } # => [0, 1, 2, 3, 4, 5]
- * a # => [0, 1, 2, 3, 4, 5]
+ * a.take_while {|element| true } # => [0, 1, 2, 3, 4, 5]
+ * a.take_while {|element| false } # => []
*
- * With no block given, returns a new \Enumerator:
+ * With no block given, returns a new Enumerator.
*
- * [0, 1].take_while # => #<Enumerator: [0, 1]:take_while>
+ * Does not modify +self+.
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -7572,10 +7811,10 @@ rb_ary_take_while(VALUE ary)
/*
* call-seq:
- * array.drop(n) -> new_array
+ * drop(count) -> new_array
*
- * Returns a new \Array containing all but the first +n+ element of +self+,
- * where +n+ is a non-negative \Integer;
+ * Returns a new array containing all but the first +count+ element of +self+,
+ * where +count+ is a non-negative integer;
* does not modify +self+.
*
* Examples:
@@ -7584,7 +7823,9 @@ rb_ary_take_while(VALUE ary)
* a.drop(0) # => [0, 1, 2, 3, 4, 5]
* a.drop(1) # => [1, 2, 3, 4, 5]
* a.drop(2) # => [2, 3, 4, 5]
+ * a.drop(9) # => []
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -7603,23 +7844,20 @@ rb_ary_drop(VALUE ary, VALUE n)
/*
* call-seq:
- * array.drop_while {|element| ... } -> new_array
- * array.drop_while -> new_enumerator
-
- * Returns a new \Array containing zero or more trailing elements of +self+;
- * does not modify +self+.
+ * drop_while {|element| ... } -> new_array
+ * drop_while -> new_enumerator
*
* With a block given, calls the block with each successive element of +self+;
* stops if the block returns +false+ or +nil+;
- * returns a new \Array _omitting_ those elements for which the block returned a truthy value:
+ * returns a new array _omitting_ those elements for which the block returned a truthy value;
+ * does not modify +self+:
*
* a = [0, 1, 2, 3, 4, 5]
* a.drop_while {|element| element < 3 } # => [3, 4, 5]
*
- * With no block given, returns a new \Enumerator:
- *
- * [0, 1].drop_while # => # => #<Enumerator: [0, 1]:drop_while>
+ * With no block given, returns a new Enumerator.
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -7636,38 +7874,41 @@ rb_ary_drop_while(VALUE ary)
/*
* call-seq:
- * array.any? -> true or false
- * array.any? {|element| ... } -> true or false
- * array.any?(obj) -> true or false
+ * any? -> true or false
+ * any?(object) -> true or false
+ * any? {|element| ... } -> true or false
*
- * Returns +true+ if any element of +self+ meets a given criterion.
+ * Returns whether for any element of +self+, a given criterion is satisfied.
*
- * If +self+ has no element, returns +false+ and argument or block
- * are not used.
+ * With no block and no argument, returns whether any element of +self+ is truthy:
*
- * With no block given and no argument, returns +true+ if +self+ has any truthy element,
- * +false+ otherwise:
+ * [nil, false, []].any? # => true # Array object is truthy.
+ * [nil, false, {}].any? # => true # Hash object is truthy.
+ * [nil, false, ''].any? # => true # String object is truthy.
+ * [nil, false].any? # => false # Nil and false are not truthy.
*
- * [nil, 0, false].any? # => true
- * [nil, false].any? # => false
- * [].any? # => false
+ * With argument +object+ given,
+ * returns whether <tt>object === ele</tt> for any element +ele+ in +self+:
*
- * With a block given and no argument, calls the block with each element in +self+;
- * returns +true+ if the block returns any truthy value, +false+ otherwise:
+ * [nil, false, 0].any?(0) # => true
+ * [nil, false, 1].any?(0) # => false
+ * [nil, false, 'food'].any?(/foo/) # => true
+ * [nil, false, 'food'].any?(/bar/) # => false
*
- * [0, 1, 2].any? {|element| element > 1 } # => true
- * [0, 1, 2].any? {|element| element > 2 } # => false
+ * With a block given,
+ * calls the block with each element in +self+;
+ * returns whether the block returns any truthy value:
*
- * If argument +obj+ is given, returns +true+ if +obj+.<tt>===</tt> any element,
- * +false+ otherwise:
+ * [0, 1, 2].any? {|ele| ele < 1 } # => true
+ * [0, 1, 2].any? {|ele| ele < 0 } # => false
+ *
+ * With both a block and argument +object+ given,
+ * ignores the block and uses +object+ as above.
*
- * ['food', 'drink'].any?(/foo/) # => true
- * ['food', 'drink'].any?(/bar/) # => false
- * [].any?(/foo/) # => false
- * [0, 1, 2].any?(1) # => true
- * [0, 1, 2].any?(3) # => false
+ * <b>Special case</b>: returns +false+ if +self+ is empty
+ * (regardless of any given argument or block).
*
- * Related: Enumerable#any?
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -7700,37 +7941,41 @@ rb_ary_any_p(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.all? -> true or false
- * array.all? {|element| ... } -> true or false
- * array.all?(obj) -> true or false
+ * all? -> true or false
+ * all?(object) -> true or false
+ * all? {|element| ... } -> true or false
*
- * Returns +true+ if all elements of +self+ meet a given criterion.
+ * Returns whether for every element of +self+,
+ * a given criterion is satisfied.
*
- * If +self+ has no element, returns +true+ and argument or block
- * are not used.
+ * With no block and no argument,
+ * returns whether every element of +self+ is truthy:
*
- * With no block given and no argument, returns +true+ if +self+ contains only truthy elements,
- * +false+ otherwise:
+ * [[], {}, '', 0, 0.0, Object.new].all? # => true # All truthy objects.
+ * [[], {}, '', 0, 0.0, nil].all? # => false # nil is not truthy.
+ * [[], {}, '', 0, 0.0, false].all? # => false # false is not truthy.
+ *
+ * With argument +object+ given, returns whether <tt>object === ele</tt>
+ * for every element +ele+ in +self+:
*
- * [0, 1, :foo].all? # => true
- * [0, nil, 2].all? # => false
- * [].all? # => true
+ * [0, 0, 0].all?(0) # => true
+ * [0, 1, 2].all?(1) # => false
+ * ['food', 'fool', 'foot'].all?(/foo/) # => true
+ * ['food', 'drink'].all?(/foo/) # => false
*
- * With a block given and no argument, calls the block with each element in +self+;
- * returns +true+ if the block returns only truthy values, +false+ otherwise:
+ * With a block given, calls the block with each element in +self+;
+ * returns whether the block returns only truthy values:
*
- * [0, 1, 2].all? { |element| element < 3 } # => true
- * [0, 1, 2].all? { |element| element < 2 } # => false
+ * [0, 1, 2].all? { |ele| ele < 3 } # => true
+ * [0, 1, 2].all? { |ele| ele < 2 } # => false
*
- * If argument +obj+ is given, returns +true+ if <tt>obj.===</tt> every element, +false+ otherwise:
+ * With both a block and argument +object+ given,
+ * ignores the block and uses +object+ as above.
*
- * ['food', 'fool', 'foot'].all?(/foo/) # => true
- * ['food', 'drink'].all?(/bar/) # => false
- * [].all?(/foo/) # => true
- * [0, 0, 0].all?(0) # => true
- * [0, 1, 2].all?(1) # => false
+ * <b>Special case</b>: returns +true+ if +self+ is empty
+ * (regardless of any given argument or block).
*
- * Related: Enumerable#all?
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -7763,34 +8008,35 @@ rb_ary_all_p(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.none? -> true or false
- * array.none? {|element| ... } -> true or false
- * array.none?(obj) -> true or false
+ * none? -> true or false
+ * none?(object) -> true or false
+ * none? {|element| ... } -> true or false
*
- * Returns +true+ if no element of +self+ meet a given criterion.
+ * Returns +true+ if no element of +self+ meets a given criterion, +false+ otherwise.
*
* With no block given and no argument, returns +true+ if +self+ has no truthy elements,
* +false+ otherwise:
*
- * [nil, false].none? # => true
+ * [nil, false].none? # => true
* [nil, 0, false].none? # => false
- * [].none? # => true
+ * [].none? # => true
*
- * With a block given and no argument, calls the block with each element in +self+;
+ * With argument +object+ given, returns +false+ if for any element +element+,
+ * <tt>object === element</tt>; +true+ otherwise:
+ *
+ * ['food', 'drink'].none?(/bar/) # => true
+ * ['food', 'drink'].none?(/foo/) # => false
+ * [].none?(/foo/) # => true
+ * [0, 1, 2].none?(3) # => true
+ * [0, 1, 2].none?(1) # => false
+ *
+ * With a block given, calls the block with each element in +self+;
* returns +true+ if the block returns no truthy value, +false+ otherwise:
*
* [0, 1, 2].none? {|element| element > 3 } # => true
* [0, 1, 2].none? {|element| element > 1 } # => false
*
- * If argument +obj+ is given, returns +true+ if <tt>obj.===</tt> no element, +false+ otherwise:
- *
- * ['food', 'drink'].none?(/bar/) # => true
- * ['food', 'drink'].none?(/foo/) # => false
- * [].none?(/foo/) # => true
- * [0, 1, 2].none?(3) # => true
- * [0, 1, 2].none?(1) # => false
- *
- * Related: Enumerable#none?
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -7823,9 +8069,9 @@ rb_ary_none_p(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.one? -> true or false
- * array.one? {|element| ... } -> true or false
- * array.one?(obj) -> true or false
+ * one? -> true or false
+ * one? {|element| ... } -> true or false
+ * one?(object) -> true or false
*
* Returns +true+ if exactly one element of +self+ meets a given criterion.
*
@@ -7837,14 +8083,14 @@ rb_ary_none_p(int argc, VALUE *argv, VALUE ary)
* [nil, nil].one? # => false
* [].one? # => false
*
- * With a block given and no argument, calls the block with each element in +self+;
+ * With a block given, calls the block with each element in +self+;
* returns +true+ if the block a truthy value for exactly one element, +false+ otherwise:
*
* [0, 1, 2].one? {|element| element > 0 } # => false
* [0, 1, 2].one? {|element| element > 1 } # => true
* [0, 1, 2].one? {|element| element > 2 } # => false
*
- * If argument +obj+ is given, returns +true+ if <tt>obj.===</tt> exactly one element,
+ * With argument +object+ given, returns +true+ if for exactly one element +element+, <tt>object === element</tt>;
* +false+ otherwise:
*
* [0, 1, 2].one?(0) # => true
@@ -7854,7 +8100,7 @@ rb_ary_none_p(int argc, VALUE *argv, VALUE ary)
* ['food', 'drink'].one?(/foo/) # => true
* [].one?(/foo/) # => false
*
- * Related: Enumerable#one?
+ * Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
*/
static VALUE
@@ -7897,11 +8143,11 @@ rb_ary_one_p(int argc, VALUE *argv, VALUE ary)
/*
* call-seq:
- * array.dig(index, *identifiers) -> object
+ * dig(index, *identifiers) -> object
*
- * Finds and returns the object in nested objects
- * that is specified by +index+ and +identifiers+.
- * The nested objects may be instances of various classes.
+ * Finds and returns the object in nested object
+ * specified by +index+ and +identifiers+;
+ * the nested objects may be instances of various classes.
* See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
*
* Examples:
@@ -7912,6 +8158,7 @@ rb_ary_one_p(int argc, VALUE *argv, VALUE ary)
* a.dig(1, 2, 0) # => :bat
* a.dig(1, 2, 3) # => nil
*
+ * Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
*/
static VALUE
@@ -7940,40 +8187,41 @@ finish_exact_sum(long n, VALUE r, VALUE v, int z)
/*
* call-seq:
- * array.sum(init = 0) -> object
- * array.sum(init = 0) {|element| ... } -> object
+ * sum(init = 0) -> object
+ * sum(init = 0) {|element| ... } -> object
*
- * When no block is given, returns the object equivalent to:
+ * With no block given, returns the sum of +init+ and all elements of +self+;
+ * for array +array+ and value +init+, equivalent to:
*
* sum = init
* array.each {|element| sum += element }
* sum
*
- * For example, <tt>[e1, e2, e3].sum</tt> returns <tt>init + e1 + e2 + e3</tt>.
+ * For example, <tt>[e0, e1, e2].sum</tt> returns <tt>init + e0 + e1 + e2</tt>.
*
* Examples:
*
- * a = [0, 1, 2, 3]
- * a.sum # => 6
- * a.sum(100) # => 106
+ * [0, 1, 2, 3].sum # => 6
+ * [0, 1, 2, 3].sum(100) # => 106
+ * ['abc', 'def', 'ghi'].sum('jkl') # => "jklabcdefghi"
+ * [[:foo, :bar], ['foo', 'bar']].sum([2, 3])
+ * # => [2, 3, :foo, :bar, "foo", "bar"]
*
- * The elements need not be numeric, but must be <tt>+</tt>-compatible
- * with each other and with +init+:
+ * The +init+ value and elements need not be numeric, but must all be <tt>+</tt>-compatible:
*
- * a = ['abc', 'def', 'ghi']
- * a.sum('jkl') # => "jklabcdefghi"
+ * # Raises TypeError: Array can't be coerced into Integer.
+ * [[:foo, :bar], ['foo', 'bar']].sum(2)
*
- * When a block is given, it is called with each element
- * and the block's return value (instead of the element itself) is used as the addend:
+ * With a block given, calls the block with each element of +self+;
+ * the block's return value (instead of the element itself) is used as the addend:
*
- * a = ['zero', 1, :two]
- * s = a.sum('Coerced and concatenated: ') {|element| element.to_s }
- * s # => "Coerced and concatenated: zero1two"
+ * ['zero', 1, :two].sum('Coerced and concatenated: ') {|element| element.to_s }
+ * # => "Coerced and concatenated: zero1two"
*
* Notes:
*
* - Array#join and Array#flatten may be faster than Array#sum
- * for an \Array of Strings or an \Array of Arrays.
+ * for an array of strings or an array of arrays.
* - Array#sum method may not respect method redefinition of "+" methods such as Integer#+.
*
*/
@@ -8095,6 +8343,7 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary)
return v;
}
+/* :nodoc: */
static VALUE
rb_ary_deconstruct(VALUE ary)
{
@@ -8102,15 +8351,37 @@ rb_ary_deconstruct(VALUE ary)
}
/*
- * An \Array is an ordered, integer-indexed collection of objects, called _elements_.
- * Any object (even another array) may be an array element,
- * and an array can contain objects of different types.
+ * An \Array object is an ordered, integer-indexed collection of objects,
+ * called _elements_;
+ * the object represents
+ * an {array data structure}[https://en.wikipedia.org/wiki/Array_(data_structure)].
+ *
+ * An element may be any object (even another array);
+ * elements may be any mixture of objects of different types.
+ *
+ * Important data structures that use arrays include:
+ *
+ * - {Coordinate vector}[https://en.wikipedia.org/wiki/Coordinate_vector].
+ * - {Matrix}[https://en.wikipedia.org/wiki/Matrix_(mathematics)].
+ * - {Heap}[https://en.wikipedia.org/wiki/Heap_(data_structure)].
+ * - {Hash table}[https://en.wikipedia.org/wiki/Hash_table].
+ * - {Deque (double-ended queue)}[https://en.wikipedia.org/wiki/Double-ended_queue].
+ * - {Queue}[https://en.wikipedia.org/wiki/Queue_(abstract_data_type)].
+ * - {Stack}[https://en.wikipedia.org/wiki/Stack_(abstract_data_type)].
+ *
+ * There are also array-like data structures:
+ *
+ * - {Associative array}[https://en.wikipedia.org/wiki/Associative_array] (see Hash).
+ * - {Directory}[https://en.wikipedia.org/wiki/Directory_(computing)] (see Dir).
+ * - {Environment}[https://en.wikipedia.org/wiki/Environment_variable] (see ENV).
+ * - {Set}[https://en.wikipedia.org/wiki/Set_(abstract_data_type)] (see Set).
+ * - {String}[https://en.wikipedia.org/wiki/String_(computer_science)] (see String).
*
* == \Array Indexes
*
* \Array indexing starts at 0, as in C or Java.
*
- * A positive index is an offset from the first element:
+ * A non-negative index is an offset from the first element:
*
* - Index 0 indicates the first element.
* - Index 1 indicates the second element.
@@ -8122,6 +8393,9 @@ rb_ary_deconstruct(VALUE ary)
* - Index -2 indicates the next-to-last element.
* - ...
*
+ *
+ * === In-Range and Out-of-Range Indexes
+ *
* A non-negative index is <i>in range</i> if and only if it is smaller than
* the size of the array. For a 3-element array:
*
@@ -8134,31 +8408,32 @@ rb_ary_deconstruct(VALUE ary)
* - Indexes -1 through -3 are in range.
* - Index -4 is out of range.
*
+ * === Effective Index
+ *
* Although the effective index into an array is always an integer,
- * some methods (both within and outside of class \Array)
+ * some methods (both within class \Array and elsewhere)
* accept one or more non-integer arguments that are
* {integer-convertible objects}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects].
*
- *
* == Creating Arrays
*
* You can create an \Array object explicitly with:
*
- * - An {array literal}[rdoc-ref:literals.rdoc@Array+Literals]:
+ * - An {array literal}[rdoc-ref:syntax/literals.rdoc@Array+Literals]:
*
* [1, 'one', :one, [2, 'two', :two]]
*
- * - A {%w or %W: string-array Literal}[rdoc-ref:literals.rdoc@25w+and+-25W-3A+String-Array+Literals]:
+ * - A {%w or %W string-array Literal}[rdoc-ref:syntax/literals.rdoc@25w+and+-25W-3A+String-Array+Literals]:
*
* %w[foo bar baz] # => ["foo", "bar", "baz"]
* %w[1 % *] # => ["1", "%", "*"]
*
- * - A {%i pr %I: symbol-array Literal}[rdoc-ref:literals.rdoc@25i+and+-25I-3A+Symbol-Array+Literals]:
+ * - A {%i or %I symbol-array Literal}[rdoc-ref:syntax/literals.rdoc@25i+and+-25I-3A+Symbol-Array+Literals]:
*
* %i[foo bar baz] # => [:foo, :bar, :baz]
* %i[1 % *] # => [:"1", :%, :*]
*
- * - \Method Kernel#Array:
+ * - Method Kernel#Array:
*
* Array(["a", "b"]) # => ["a", "b"]
* Array(1..5) # => [1, 2, 3, 4, 5]
@@ -8167,7 +8442,7 @@ rb_ary_deconstruct(VALUE ary)
* Array(1) # => [1]
* Array({:a => "a", :b => "b"}) # => [[:a, "a"], [:b, "b"]]
*
- * - \Method Array.new:
+ * - Method Array.new:
*
* Array.new # => []
* Array.new(3) # => [nil, nil, nil]
@@ -8222,8 +8497,8 @@ rb_ary_deconstruct(VALUE ary)
*
* == Example Usage
*
- * In addition to the methods it mixes in through the Enumerable module, the
- * \Array class has proprietary methods for accessing, searching and otherwise
+ * In addition to the methods it mixes in through the Enumerable module,
+ * class \Array has proprietary methods for accessing, searching and otherwise
* manipulating arrays.
*
* Some of the more common ones are illustrated below.
@@ -8273,7 +8548,7 @@ rb_ary_deconstruct(VALUE ary)
*
* == Obtaining Information about an \Array
*
- * Arrays keep track of their own length at all times. To query an array
+ * An array keeps track of its own length at all times. To query an array
* about the number of elements it contains, use #length, #count or #size.
*
* browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
@@ -8288,7 +8563,7 @@ rb_ary_deconstruct(VALUE ary)
*
* browsers.include?('Konqueror') #=> false
*
- * == Adding Items to Arrays
+ * == Adding Items to an \Array
*
* Items can be added to the end of an array by using either #push or #<<
*
@@ -8349,11 +8624,11 @@ rb_ary_deconstruct(VALUE ary)
* arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
* arr.uniq #=> [2, 5, 6, 556, 8, 9, 0, 123]
*
- * == Iterating over Arrays
+ * == Iterating over an \Array
*
- * Like all classes that include the Enumerable module, \Array has an each
+ * Like all classes that include the Enumerable module, class \Array has an each
* method, which defines what elements should be iterated over and how. In
- * case of Array's #each, all elements in the \Array instance are yielded to
+ * case of Array#each, all elements in +self+ are yielded to
* the supplied block in sequence.
*
* Note that this operation leaves the array unchanged.
@@ -8413,7 +8688,7 @@ rb_ary_deconstruct(VALUE ary)
*
* == What's Here
*
- * First, what's elsewhere. \Class \Array:
+ * First, what's elsewhere. Class \Array:
*
* - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here].
* - Includes {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here],
@@ -8438,148 +8713,132 @@ rb_ary_deconstruct(VALUE ary)
* - ::new: Returns a new array.
* - ::try_convert: Returns a new array created from a given object.
*
+ * See also {Creating Arrays}[rdoc-ref:Array@Creating+Arrays].
+ *
* === Methods for Querying
*
- * - #length, #size: Returns the count of elements.
- * - #include?: Returns whether any element <tt>==</tt> a given object.
- * - #empty?: Returns whether there are no elements.
* - #all?: Returns whether all elements meet a given criterion.
* - #any?: Returns whether any element meets a given criterion.
+ * - #count: Returns the count of elements that meet a given criterion.
+ * - #empty?: Returns whether there are no elements.
+ * - #find_index (aliased as #index): Returns the index of the first element that meets a given criterion.
+ * - #hash: Returns the integer hash code.
+ * - #include?: Returns whether any element <tt>==</tt> a given object.
+ * - #length (aliased as #size): Returns the count of elements.
* - #none?: Returns whether no element <tt>==</tt> a given object.
* - #one?: Returns whether exactly one element <tt>==</tt> a given object.
- * - #count: Returns the count of elements that meet a given criterion.
- * - #find_index, #index: Returns the index of the first element that meets a given criterion.
* - #rindex: Returns the index of the last element that meets a given criterion.
- * - #hash: Returns the integer hash code.
*
* === Methods for Comparing
*
- * - #<=>: Returns -1, 0, or 1 * as +self+ is less than, equal to, or
- * greater than a given object.
- * - #==: Returns whether each element in +self+ is <tt>==</tt> to the corresponding element
- * in a given object.
- * - #eql?: Returns whether each element in +self+ is <tt>eql?</tt> to the corresponding
- * element in a given object.
+ * - #<=>: Returns -1, 0, or 1, as +self+ is less than, equal to, or greater than a given object.
+ * - #==: Returns whether each element in +self+ is <tt>==</tt> to the corresponding element in a given object.
+ * - #eql?: Returns whether each element in +self+ is <tt>eql?</tt> to the corresponding element in a given object.
* === Methods for Fetching
*
* These methods do not modify +self+.
*
- * - #[]: Returns one or more elements.
- * - #fetch: Returns the element at a given offset.
- * - #first: Returns one or more leading elements.
- * - #last: Returns one or more trailing elements.
- * - #max: Returns one or more maximum-valued elements,
- * as determined by <tt><=></tt> or a given block.
- * - #min: Returns one or more minimum-valued elements,
- * as determined by <tt><=></tt> or a given block.
- * - #minmax: Returns the minimum-valued and maximum-valued elements,
- * as determined by <tt><=></tt> or a given block.
- * - #assoc: Returns the first element that is an array
- * whose first element <tt>==</tt> a given object.
- * - #rassoc: Returns the first element that is an array
- * whose second element <tt>==</tt> a given object.
+ * - #[] (aliased as #slice): Returns consecutive elements as determined by a given argument.
+ * - #assoc: Returns the first element that is an array whose first element <tt>==</tt> a given object.
* - #at: Returns the element at a given offset.
- * - #values_at: Returns the elements at given offsets.
- * - #dig: Returns the object in nested objects
- * that is specified by a given index and additional arguments.
+ * - #bsearch: Returns an element selected via a binary search as determined by a given block.
+ * - #bsearch_index: Returns the index of an element selected via a binary search as determined by a given block.
+ * - #compact: Returns an array containing all non-+nil+ elements.
+ * - #dig: Returns the object in nested objects that is specified by a given index and additional arguments.
* - #drop: Returns trailing elements as determined by a given index.
- * - #take: Returns leading elements as determined by a given index.
* - #drop_while: Returns trailing elements as determined by a given block.
- * - #take_while: Returns leading elements as determined by a given block.
- * - #slice: Returns consecutive elements as determined by a given argument.
- * - #sort: Returns all elements in an order determined by <tt><=></tt> or a given block.
+ * - #fetch: Returns the element at a given offset.
+ * - #fetch_values: Returns elements at given offsets.
+ * - #first: Returns one or more leading elements.
+ * - #last: Returns one or more trailing elements.
+ * - #max: Returns one or more maximum-valued elements, as determined by <tt>#<=></tt> or a given block.
+ * - #min: Returns one or more minimum-valued elements, as determined by <tt>#<=></tt> or a given block.
+ * - #minmax: Returns the minimum-valued and maximum-valued elements, as determined by <tt>#<=></tt> or a given block.
+ * - #rassoc: Returns the first element that is an array whose second element <tt>==</tt> a given object.
+ * - #reject: Returns an array containing elements not rejected by a given block.
* - #reverse: Returns all elements in reverse order.
- * - #compact: Returns an array containing all non-+nil+ elements.
- * - #select, #filter: Returns an array containing elements selected by a given block.
- * - #uniq: Returns an array containing non-duplicate elements.
* - #rotate: Returns all elements with some rotated from one end to the other.
- * - #bsearch: Returns an element selected via a binary search
- * as determined by a given block.
- * - #bsearch_index: Returns the index of an element selected via a binary search
- * as determined by a given block.
* - #sample: Returns one or more random elements.
+ * - #select (aliased as #filter): Returns an array containing elements selected by a given block.
* - #shuffle: Returns elements in a random order.
+ * - #sort: Returns all elements in an order determined by <tt>#<=></tt> or a given block.
+ * - #take: Returns leading elements as determined by a given index.
+ * - #take_while: Returns leading elements as determined by a given block.
+ * - #uniq: Returns an array containing non-duplicate elements.
+ * - #values_at: Returns the elements at given offsets.
*
* === Methods for Assigning
*
* These methods add, replace, or reorder elements in +self+.
*
+ * - #<<: Appends an element.
* - #[]=: Assigns specified elements with a given object.
- * - #push, #append, #<<: Appends trailing elements.
- * - #unshift, #prepend: Prepends leading elements.
- * - #insert: Inserts given objects at a given offset; does not replace elements.
* - #concat: Appends all elements from given arrays.
* - #fill: Replaces specified elements with specified objects.
- * - #replace: Replaces the content of +self+ with the content of a given array.
+ * - #flatten!: Replaces each nested array in +self+ with the elements from that array.
+ * - #initialize_copy (aliased as #replace): Replaces the content of +self+ with the content of a given array.
+ * - #insert: Inserts given objects at a given offset; does not replace elements.
+ * - #push (aliased as #append): Appends elements.
* - #reverse!: Replaces +self+ with its elements reversed.
* - #rotate!: Replaces +self+ with its elements rotated.
* - #shuffle!: Replaces +self+ with its elements in random order.
- * - #sort!: Replaces +self+ with its elements sorted,
- * as determined by <tt><=></tt> or a given block.
+ * - #sort!: Replaces +self+ with its elements sorted, as determined by <tt>#<=></tt> or a given block.
* - #sort_by!: Replaces +self+ with its elements sorted, as determined by a given block.
+ * - #unshift (aliased as #prepend): Prepends leading elements.
*
* === Methods for Deleting
*
* Each of these methods removes elements from +self+:
*
- * - #pop: Removes and returns the last element.
- * - #shift: Removes and returns the first element.
+ * - #clear: Removes all elements.
* - #compact!: Removes all +nil+ elements.
* - #delete: Removes elements equal to a given object.
* - #delete_at: Removes the element at a given offset.
* - #delete_if: Removes elements specified by a given block.
* - #keep_if: Removes elements not specified by a given block.
+ * - #pop: Removes and returns the last element.
* - #reject!: Removes elements specified by a given block.
- * - #select!, #filter!: Removes elements not specified by a given block.
+ * - #select! (aliased as #filter!): Removes elements not specified by a given block.
+ * - #shift: Removes and returns the first element.
* - #slice!: Removes and returns a sequence of elements.
* - #uniq!: Removes duplicates.
*
* === Methods for Combining
*
* - #&: Returns an array containing elements found both in +self+ and a given array.
- * - #intersection: Returns an array containing elements found both in +self+
- * and in each given array.
* - #+: Returns an array containing all elements of +self+ followed by all elements of a given array.
* - #-: Returns an array containing all elements of +self+ that are not found in a given array.
- * - #|: Returns an array containing all elements of +self+ and all elements of a given array,
- * duplicates removed.
- * - #union: Returns an array containing all elements of +self+ and all elements of given arrays,
- * duplicates removed.
- * - #difference: Returns an array containing all elements of +self+ that are not found
- * in any of the given arrays..
+ * - #|: Returns an array containing all element of +self+ and all elements of a given array, duplicates removed.
+ * - #difference: Returns an array containing all elements of +self+ that are not found in any of the given arrays..
+ * - #intersection: Returns an array containing elements found both in +self+ and in each given array.
* - #product: Returns or yields all combinations of elements from +self+ and given arrays.
+ * - #reverse: Returns an array containing all elements of +self+ in reverse order.
+ * - #union: Returns an array containing all elements of +self+ and all elements of given arrays, duplicates removed.
*
* === Methods for Iterating
*
+ * - #combination: Calls a given block with combinations of elements of +self+; a combination does not use the same element more than once.
+ * - #cycle: Calls a given block with each element, then does so again, for a specified number of times, or forever.
* - #each: Passes each element to a given block.
- * - #reverse_each: Passes each element, in reverse order, to a given block.
* - #each_index: Passes each element index to a given block.
- * - #cycle: Calls a given block with each element, then does so again,
- * for a specified number of times, or forever.
- * - #combination: Calls a given block with combinations of elements of +self+;
- * a combination does not use the same element more than once.
- * - #permutation: Calls a given block with permutations of elements of +self+;
- * a permutation does not use the same element more than once.
- * - #repeated_combination: Calls a given block with combinations of elements of +self+;
- * a combination may use the same element more than once.
- * - #repeated_permutation: Calls a given block with permutations of elements of +self+;
- * a permutation may use the same element more than once.
+ * - #permutation: Calls a given block with permutations of elements of +self+; a permutation does not use the same element more than once.
+ * - #repeated_combination: Calls a given block with combinations of elements of +self+; a combination may use the same element more than once.
+ * - #repeated_permutation: Calls a given block with permutations of elements of +self+; a permutation may use the same element more than once.
+ * - #reverse_each: Passes each element, in reverse order, to a given block.
*
* === Methods for Converting
*
- * - #map, #collect: Returns an array containing the block return-value for each element.
- * - #map!, #collect!: Replaces each element with a block return-value.
+ * - #collect (aliased as #map): Returns an array containing the block return-value for each element.
+ * - #collect! (aliased as #map!): Replaces each element with a block return-value.
* - #flatten: Returns an array that is a recursive flattening of +self+.
- * - #flatten!: Replaces each nested array in +self+ with the elements from that array.
- * - #inspect, #to_s: Returns a new String containing the elements.
- * - #join: Returns a newsString containing the elements joined by the field separator.
+ * - #inspect (aliased as #to_s): Returns a new String containing the elements.
+ * - #join: Returns a new String containing the elements joined by the field separator.
* - #to_a: Returns +self+ or a new array containing all elements.
* - #to_ary: Returns +self+.
* - #to_h: Returns a new hash formed from the elements.
* - #transpose: Transposes +self+, which must be an array of arrays.
- * - #zip: Returns a new array of arrays containing +self+ and given arrays;
- * follow the link for details.
+ * - #zip: Returns a new array of arrays containing +self+ and given arrays.
*
* === Other Methods
*
@@ -8590,7 +8849,6 @@ rb_ary_deconstruct(VALUE ary)
* - With string argument +field_separator+, a new string that is equivalent to
* <tt>join(field_separator)</tt>.
*
- * - #abbrev: Returns a hash of unambiguous abbreviations for elements.
* - #pack: Packs the elements into a binary sequence.
* - #sum: Returns a sum of elements according to either <tt>+</tt> or a given block.
*/
@@ -8598,6 +8856,8 @@ rb_ary_deconstruct(VALUE ary)
void
Init_Array(void)
{
+ fake_ary_flags = init_fake_ary_flags();
+
rb_cArray = rb_define_class("Array", rb_cObject);
rb_include_module(rb_cArray, rb_mEnumerable);
@@ -8641,6 +8901,9 @@ Init_Array(void)
rb_define_method(rb_cArray, "length", rb_ary_length, 0);
rb_define_method(rb_cArray, "size", rb_ary_length, 0);
rb_define_method(rb_cArray, "empty?", rb_ary_empty_p, 0);
+ rb_define_method(rb_cArray, "find", rb_ary_find, -1);
+ rb_define_method(rb_cArray, "detect", rb_ary_find, -1);
+ rb_define_method(rb_cArray, "rfind", rb_ary_rfind, -1);
rb_define_method(rb_cArray, "find_index", rb_ary_index, -1);
rb_define_method(rb_cArray, "index", rb_ary_index, -1);
rb_define_method(rb_cArray, "rindex", rb_ary_rindex, -1);
@@ -8718,8 +8981,12 @@ Init_Array(void)
rb_define_method(rb_cArray, "one?", rb_ary_one_p, -1);
rb_define_method(rb_cArray, "dig", rb_ary_dig, -1);
rb_define_method(rb_cArray, "sum", rb_ary_sum, -1);
+ rb_define_method(rb_cArray, "freeze", rb_ary_freeze, 0);
rb_define_method(rb_cArray, "deconstruct", rb_ary_deconstruct, 0);
+
+ rb_cArray_empty_frozen = RB_OBJ_SET_SHAREABLE(rb_ary_freeze(rb_ary_new()));
+ rb_vm_register_global_object(rb_cArray_empty_frozen);
}
#include "array.rbinc"