Add Array#delete_if
[mruby.git] / mrbgems / mruby-array-ext / src / array.c
blobad6cead2c63c4d7f5758371892c9975543b19c4b
1 #include "mruby.h"
2 #include "mruby/value.h"
3 #include "mruby/array.h"
5 /*
6 * call-seq:
7 * Array.try_convert(obj) -> array or nil
9 * Try to convert <i>obj</i> into an array, using +to_ary+ method.
10 * Returns converted array or +nil+ if <i>obj</i> cannot be converted
11 * for any reason. This method can be used to check if an argument is an
12 * array.
14 * Array.try_convert([1]) #=> [1]
15 * Array.try_convert("1") #=> nil
17 * if tmp = Array.try_convert(arg)
18 * # the argument is an array
19 * elsif tmp = String.try_convert(arg)
20 * # the argument is a string
21 * end
25 static mrb_value
26 mrb_ary_s_try_convert(mrb_state *mrb, mrb_value self)
28 mrb_value ary;
30 mrb_get_args(mrb, "o", &ary);
31 return mrb_check_array_type(mrb, ary);
35 * call-seq:
36 * ary.assoc(obj) -> new_ary or nil
38 * Searches through an array whose elements are also arrays
39 * comparing _obj_ with the first element of each contained array
40 * using obj.==.
41 * Returns the first contained array that matches (that
42 * is, the first associated array),
43 * or +nil+ if no match is found.
44 * See also <code>Array#rassoc</code>.
46 * s1 = [ "colors", "red", "blue", "green" ]
47 * s2 = [ "letters", "a", "b", "c" ]
48 * s3 = "foo"
49 * a = [ s1, s2, s3 ]
50 * a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
51 * a.assoc("foo") #=> nil
54 static mrb_value
55 mrb_ary_assoc(mrb_state *mrb, mrb_value ary)
57 mrb_int i;
58 mrb_value v, k;
60 mrb_get_args(mrb, "o", &k);
62 for (i = 0; i < RARRAY_LEN(ary); ++i) {
63 v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]);
64 if (!mrb_nil_p(v) && RARRAY_LEN(v) > 0 &&
65 mrb_equal(mrb, RARRAY_PTR(v)[0], k))
66 return v;
68 return mrb_nil_value();
72 * call-seq:
73 * ary.rassoc(obj) -> new_ary or nil
75 * Searches through the array whose elements are also arrays. Compares
76 * _obj_ with the second element of each contained array using
77 * <code>==</code>. Returns the first contained array that matches. See
78 * also <code>Array#assoc</code>.
80 * a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
81 * a.rassoc("two") #=> [2, "two"]
82 * a.rassoc("four") #=> nil
85 static mrb_value
86 mrb_ary_rassoc(mrb_state *mrb, mrb_value ary)
88 mrb_int i;
89 mrb_value v, value;
91 mrb_get_args(mrb, "o", &value);
93 for (i = 0; i < RARRAY_LEN(ary); ++i) {
94 v = RARRAY_PTR(ary)[i];
95 if (mrb_type(v) == MRB_TT_ARRAY &&
96 RARRAY_LEN(v) > 1 &&
97 mrb_equal(mrb, RARRAY_PTR(v)[1], value))
98 return v;
100 return mrb_nil_value();
104 * call-seq:
105 * ary.at(index) -> obj or nil
107 * Returns the element at _index_. A
108 * negative index counts from the end of +self+. Returns +nil+
109 * if the index is out of range. See also <code>Array#[]</code>.
111 * a = [ "a", "b", "c", "d", "e" ]
112 * a.at(0) #=> "a"
113 * a.at(-1) #=> "e"
116 static mrb_value
117 mrb_ary_at(mrb_state *mrb, mrb_value ary)
119 mrb_int pos;
120 mrb_get_args(mrb, "i", &pos);
122 return mrb_ary_entry(ary, pos);
125 void
126 mrb_mruby_array_ext_gem_init(mrb_state* mrb)
128 struct RClass * a = mrb->array_class;
130 mrb_define_class_method(mrb, a, "try_convert", mrb_ary_s_try_convert, MRB_ARGS_REQ(1));
132 mrb_define_method(mrb, a, "assoc", mrb_ary_assoc, MRB_ARGS_REQ(1));
133 mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1));
134 mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1));
137 void
138 mrb_mruby_array_ext_gem_final(mrb_state* mrb)