summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-hash-ext/mrblib/hash.rb
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-hash-ext/mrblib/hash.rb')
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb
index 723a0b907..bf3836514 100644
--- a/mrbgems/mruby-hash-ext/mrblib/hash.rb
+++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb
@@ -13,4 +13,18 @@ class Hash
alias each_pair each
alias update merge!
+
+ def fetch(key, none=NONE, &block)
+ unless self.key?(key)
+ if block
+ block.call
+ elsif none != NONE
+ none
+ else
+ raise RuntimeError, "Key not found: #{key}"
+ end
+ else
+ self[key]
+ end
+ end
end