mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
@@ -2,6 +2,7 @@
|
||||
#include "mruby/value.h"
|
||||
#include "mruby/array.h"
|
||||
#include "mruby/range.h"
|
||||
#include "mruby/hash.h"
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
@@ -105,6 +106,48 @@ mrb_ary_values_at(mrb_state *mrb, mrb_value self)
|
||||
return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* ary.to_h -> Hash
|
||||
*
|
||||
* Returns the result of interpreting <i>aray</i> as an array of
|
||||
* <tt>[key, value]</tt> paris.
|
||||
*
|
||||
* [[:foo, :bar], [1, 2]].to_h
|
||||
* # => {:foo => :bar, 1 => 2}
|
||||
*/
|
||||
|
||||
static mrb_value
|
||||
mrb_ary_to_h(mrb_state *mrb, mrb_value ary)
|
||||
{
|
||||
mrb_int i;
|
||||
mrb_value v, hash;
|
||||
|
||||
hash = mrb_hash_new_capa(mrb, 0);
|
||||
|
||||
for (i = 0; i < RARRAY_LEN(ary); ++i) {
|
||||
v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]);
|
||||
|
||||
if (mrb_nil_p(v)) {
|
||||
mrb_raisef(mrb, E_TYPE_ERROR, "wrong element type %S at %S (expected array)",
|
||||
mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, RARRAY_PTR(ary)[i])),
|
||||
mrb_fixnum_value(i)
|
||||
);
|
||||
}
|
||||
|
||||
if (RARRAY_LEN(v) != 2) {
|
||||
mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong array length at %S (expected 2, was %S)",
|
||||
mrb_fixnum_value(i),
|
||||
mrb_fixnum_value(RARRAY_LEN(v))
|
||||
);
|
||||
}
|
||||
|
||||
mrb_hash_set(mrb, hash, RARRAY_PTR(v)[0], RARRAY_PTR(v)[1]);
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_array_ext_gem_init(mrb_state* mrb)
|
||||
{
|
||||
@@ -114,6 +157,7 @@ mrb_mruby_array_ext_gem_init(mrb_state* mrb)
|
||||
mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1));
|
||||
mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1));
|
||||
mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY());
|
||||
mrb_define_method(mrb, a, "to_h", mrb_ary_to_h, MRB_ARGS_REQ(0));
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -285,3 +285,11 @@ assert('Array#values_at') do
|
||||
assert_equal ['none', nil, nil, 'red', 'green', 'purple'], a.values_at(4..6, 0...3)
|
||||
assert_raise(TypeError) { a.values_at 'tt' }
|
||||
end
|
||||
|
||||
assert('Array#to_h') do
|
||||
assert_equal({}, [].to_h)
|
||||
assert_equal({a: 1, b:2}, [[:a, 1], [:b, 2]].to_h)
|
||||
|
||||
assert_raise(TypeError) { [1].to_h }
|
||||
assert_raise(ArgumentError) { [[1]].to_h }
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user