* hash.c: Add Hash#to_h [Feature #6276]
[rubyspec:84b7fe3f24d2] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35339 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0c7163ae5e
commit
f92be2c01a
2
NEWS
2
NEWS
@ -22,6 +22,8 @@ with all sufficient information, see the ChangeLog file.
|
|||||||
* added Enumerable#lazy method for lazy enumeration.
|
* added Enumerable#lazy method for lazy enumeration.
|
||||||
|
|
||||||
* Hash
|
* Hash
|
||||||
|
* added method:
|
||||||
|
* added Hash#to_h as explicit conversion method, like Array#to_a.
|
||||||
* extended method:
|
* extended method:
|
||||||
* Hash#default_proc= can be passed nil to clear the default proc.
|
* Hash#default_proc= can be passed nil to clear the default proc.
|
||||||
|
|
||||||
|
25
hash.c
25
hash.c
@ -1452,6 +1452,30 @@ rb_hash_to_hash(VALUE hash)
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* hsh.to_h -> hsh or new_hash
|
||||||
|
*
|
||||||
|
* Returns +self+. If called on a subclass of Hash, converts
|
||||||
|
* the receiver to a Hash object.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_hash_to_h(VALUE hash)
|
||||||
|
{
|
||||||
|
if (rb_obj_class(hash) != rb_cHash) {
|
||||||
|
VALUE ret = rb_hash_new();
|
||||||
|
if (!RHASH_EMPTY_P(hash))
|
||||||
|
RHASH(ret)->ntbl = st_copy(RHASH(hash)->ntbl);
|
||||||
|
if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
|
||||||
|
FL_SET(ret, HASH_PROC_DEFAULT);
|
||||||
|
}
|
||||||
|
RHASH_IFNONE(ret) = RHASH_IFNONE(hash);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
keys_i(VALUE key, VALUE value, VALUE ary)
|
keys_i(VALUE key, VALUE value, VALUE ary)
|
||||||
{
|
{
|
||||||
@ -3333,6 +3357,7 @@ Init_Hash(void)
|
|||||||
rb_define_method(rb_cHash,"rehash", rb_hash_rehash, 0);
|
rb_define_method(rb_cHash,"rehash", rb_hash_rehash, 0);
|
||||||
|
|
||||||
rb_define_method(rb_cHash,"to_hash", rb_hash_to_hash, 0);
|
rb_define_method(rb_cHash,"to_hash", rb_hash_to_hash, 0);
|
||||||
|
rb_define_method(rb_cHash,"to_h", rb_hash_to_h, 0);
|
||||||
rb_define_method(rb_cHash,"to_a", rb_hash_to_a, 0);
|
rb_define_method(rb_cHash,"to_a", rb_hash_to_a, 0);
|
||||||
rb_define_method(rb_cHash,"inspect", rb_hash_inspect, 0);
|
rb_define_method(rb_cHash,"inspect", rb_hash_inspect, 0);
|
||||||
rb_define_alias(rb_cHash, "to_s", "inspect");
|
rb_define_alias(rb_cHash, "to_s", "inspect");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user