method
to_h
v2_4_6 -
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();
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;
} Related methods
- Instance methods
- &
- *
- +
- -
- <<
- <=>
- ==
- []
- []=
- abbrev
- any?
- assoc
- at
- bsearch
- bsearch_index
- clear
- collect
- collect!
- combination
- compact
- compact!
- concat
- count
- cycle
- dclone
- delete
- delete_at
- delete_if
- dig
- drop
- drop_while
- each
- each_index
- empty?
- eql?
- fetch
- fill
- find_index
- first
- flatten
- flatten!
- frozen?
- hash
- include?
- index
- initialize_copy
- insert
- inspect
- join
- keep_if
- last
- length
- map
- map!
- max
- min
- pack
- permutation
- pop
- pretty_print
- pretty_print_cycle
- product
- push
- rassoc
- reject
- reject!
- repeated_combination
- repeated_permutation
- replace
- reverse
- reverse!
- reverse_each
- rindex
- rotate
- rotate!
- sample
- select
- select!
- shelljoin
- shift
- shuffle
- shuffle!
- size
- slice
- slice!
- sort
- sort!
- sort_by!
- sum
- take
- take_while
- to_a
- to_ary
- to_csv
- to_h
- to_s
- transpose
- uniq
- uniq!
- unshift
- values_at
- zip
- |
- Class methods
- []
- new
- try_convert