From 172060877a80d5f1c5bd725dafd041c7180e9a28 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Wed, 4 Jun 2014 18:57:31 +0900 Subject: add Hash#to_h; ref #2348 --- mrbgems/mruby-hash-ext/mrblib/hash.rb | 11 +++++++++++ mrbgems/mruby-hash-ext/test/hash.rb | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 504848a74..9da08dc3a 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -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 diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb index 62cfc8856..2bc5b911a 100644 --- a/mrbgems/mruby-hash-ext/test/hash.rb +++ b/mrbgems/mruby-hash-ext/test/hash.rb @@ -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 -- cgit v1.2.3