summaryrefslogtreecommitdiffhomepage
path: root/mrblib/hash.rb
diff options
context:
space:
mode:
authormimaki <[email protected]>2012-04-20 09:39:03 +0900
committermimaki <[email protected]>2012-04-20 09:39:03 +0900
commite0d6430f63c4cbe0c71ce82ee23284671389a818 (patch)
tree41abad7f12eced98d9ac14d141cea62464c3332f /mrblib/hash.rb
parent54ad561098ed353ada70205c39b2c42a2a2eb9e5 (diff)
downloadmruby-e0d6430f63c4cbe0c71ce82ee23284671389a818.tar.gz
mruby-e0d6430f63c4cbe0c71ce82ee23284671389a818.zip
add mruby sources
Diffstat (limited to 'mrblib/hash.rb')
-rw-r--r--mrblib/hash.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/mrblib/hash.rb b/mrblib/hash.rb
new file mode 100644
index 000000000..7157684f8
--- /dev/null
+++ b/mrblib/hash.rb
@@ -0,0 +1,58 @@
+#
+# Hash
+#
+class Hash
+ # 15.2.13.4.8
+ def delete(key, &block)
+ if block && ! self.has_key?(key)
+ block.call(key)
+ else
+ self.__delete(key)
+ end
+ end
+
+ # 15.2.13.4.9
+ def each(&block)
+ self.keys.each{|k| block.call([k, self[k]])}
+ self
+ end
+
+ # 15.2.13.4.10
+ def each_key(&block)
+ self.keys.each{|k| block.call(k)}
+ self
+ end
+
+ # 15.2.13.4.11
+ def each_value(&block)
+ self.keys.each{|k| block.call(self[k])}
+ self
+ end
+
+ # 15.2.13.4.16
+ def initialize(*args, &block)
+ self.__init_core(block, *args)
+ end
+
+ # 15.2.13.4.22
+ def merge(other, &block)
+ h = {}
+ raise "can't convert argument into Hash" unless other.respond_to?(:to_hash)
+ other = other.to_hash
+ self.each_key{|k| h[k] = self[k]}
+ if block
+ other.each_key{|k|
+ h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
+ }
+ else
+ other.each_key{|k| h[k] = other[k]}
+ end
+ h
+ end
+end
+
+# include modules
+module Enumerable; end
+class Hash
+ include Enumerable
+end