Merge pull request #1045 from kouki-o-iij/pr-hash-ext

add mruby-hash-ext mrbgem, and method: Hash#merge!
This commit is contained in:
Yukihiro "Matz" Matsumoto
2013-03-22 18:33:31 -07:00
4 changed files with 39 additions and 0 deletions
+3
View File
@@ -35,6 +35,9 @@ MRuby::Build.new do |conf|
# Use extensional Array class
conf.gem "#{root}/mrbgems/mruby-array-ext"
# Use extensional Hash class
conf.gem "#{root}/mrbgems/mruby-hash-ext"
# Generate binaries
# conf.bins = %w(mrbc mruby mirb)
+4
View File
@@ -0,0 +1,4 @@
MRuby::Gem::Specification.new('mruby-hash-ext') do |spec|
spec.license = 'MIT'
spec.authors = 'mruby developers'
end
+12
View File
@@ -0,0 +1,12 @@
class Hash
def merge!(other, &block)
if block
other.each_key{|k|
self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
}
else
other.each_key{|k| self[k] = other[k]}
end
self
end
end
+20
View File
@@ -0,0 +1,20 @@
##
# Hash(Ext) Test
assert('Hash#merge!') do
a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
b = { 'cba_key' => 'XXX', 'xyz_key' => 'xyz_value' }
result_1 = a.merge! b
a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
result_2 = a.merge!(b) do |key, original, new|
original
end
result_1 == {'abc_key' => 'abc_value', 'cba_key' => 'XXX',
'xyz_key' => 'xyz_value' } and
result_2 == {'abc_key' => 'abc_value', 'cba_key' => 'cba_value',
'xyz_key' => 'xyz_value' }
end