diff options
| -rw-r--r-- | build_config.rb | 3 | ||||
| -rw-r--r-- | mrbgems/mruby-hash-ext/mrbgem.rake | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-hash-ext/mrblib/hash.rb | 12 | ||||
| -rw-r--r-- | mrbgems/mruby-hash-ext/test/hash.rb | 20 |
4 files changed, 39 insertions, 0 deletions
diff --git a/build_config.rb b/build_config.rb index d41c44b98..baf0a6233 100644 --- a/build_config.rb +++ b/build_config.rb @@ -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) diff --git a/mrbgems/mruby-hash-ext/mrbgem.rake b/mrbgems/mruby-hash-ext/mrbgem.rake new file mode 100644 index 000000000..3163c8c88 --- /dev/null +++ b/mrbgems/mruby-hash-ext/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-hash-ext') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' +end diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb new file mode 100644 index 000000000..3e1bac0a2 --- /dev/null +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -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 diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb new file mode 100644 index 000000000..98eb313a4 --- /dev/null +++ b/mrbgems/mruby-hash-ext/test/hash.rb @@ -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 + |
