Method: Hash#to_h

Defined in:
hash.c

#to_hself #to_h {|key, value| ... } ⇒ Object

For an instance of Hash, returns self.

For a subclass of Hash, returns a new Hash containing the content of self.

When a block is given, returns a new Hash object whose content is based on the block; the block should return a 2-element Array object specifying the key-value pair to be included in the returned Array:

h = {foo: 0, bar: 1, baz: 2}
h1 = h.to_h {|key, value| [value, key] }
h1 # => {0=>:foo, 1=>:bar, 2=>:baz}

Overloads:

  • #to_hself

    Returns:

    • (self)
  • #to_h {|key, value| ... } ⇒ Object

    Yields:



3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
# File 'hash.c', line 3569

static VALUE
rb_hash_to_h(VALUE hash)
{
    if (rb_block_given_p()) {
        return rb_hash_to_h_block(hash);
    }
    if (rb_obj_class(hash) != rb_cHash) {
        const VALUE flags = RBASIC(hash)->flags;
        hash = hash_dup(hash, rb_cHash, flags & RHASH_PROC_DEFAULT);
    }
    return hash;
}