add Hash#to_h; ref #2348

This commit is contained in:
Yukihiro "Matz" Matsumoto
2014-06-04 18:57:31 +09:00
parent 8a9e2066c3
commit 172060877a
2 changed files with 17 additions and 0 deletions
+11
View File
@@ -182,4 +182,15 @@ class Hash
end
nil
end
##
# 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.
#
def to_h
self
end
end
+6
View File
@@ -115,3 +115,9 @@ assert("Hash#key") do
assert_nil h.key('nil')
assert_equal 'nil', h.key(nil)
end
assert("Hash#to_h") do
h = { "a" => 100, "b" => 200 }
assert_equal Hash, h.to_h.class
assert_equal h, h.to_h
end