2 #include "mruby/value.h"
3 #include "mruby/array.h"
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
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
26 mrb_ary_s_try_convert(mrb_state
*mrb
, mrb_value self
)
30 mrb_get_args(mrb
, "o", &ary
);
31 return mrb_check_array_type(mrb
, ary
);
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
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" ]
50 * a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
51 * a.assoc("foo") #=> nil
55 mrb_ary_assoc(mrb_state
*mrb
, mrb_value ary
)
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
))
68 return mrb_nil_value();
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
86 mrb_ary_rassoc(mrb_state
*mrb
, mrb_value ary
)
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
&&
97 mrb_equal(mrb
, RARRAY_PTR(v
)[1], value
))
100 return mrb_nil_value();
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" ]
117 mrb_ary_at(mrb_state
*mrb
, mrb_value ary
)
120 mrb_get_args(mrb
, "i", &pos
);
122 return mrb_ary_entry(ary
, pos
);
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));
138 mrb_mruby_array_ext_gem_final(mrb_state
* mrb
)