APIdock / Ruby
/
method

to_h

v2_5_5 - Show latest stable - Class: Array
to_h()
public

Returns the result of interpreting ary as an array of [key, value] pairs.

[[:foo, :bar], [1, 2]].to_h 
 # => {:foo => :bar, 1 => 2}
static VALUE
rb_ary_to_h(VALUE ary)
{
 long i;
 VALUE hash = rb_hash_new_with_size(RARRAY_LEN(ary));
 for (i=0; i<RARRAY_LEN(ary); i++) {
 const VALUE elt = rb_ary_elt(ary, i);
 const VALUE key_value_pair = rb_check_array_type(elt);
 if (NIL_P(key_value_pair)) {
 rb_raise(rb_eTypeError, "wrong element type %"PRIsVALUE" at %ld (expected array)",
 rb_obj_class(elt), i);
 }
 if (RARRAY_LEN(key_value_pair) != 2) {
 rb_raise(rb_eArgError, "wrong array length at %ld (expected 2, was %ld)",
 i, RARRAY_LEN(key_value_pair));
 }
 rb_hash_aset(hash, RARRAY_AREF(key_value_pair, 0), RARRAY_AREF(key_value_pair, 1));
 }
 return hash;
}

AltStyle によって変換されたページ (->オリジナル) /