From 5603b8d79f3726466fdab60905c16439b59c6c3e Mon Sep 17 00:00:00 2001 From: INOUE Yasuyuki Date: Sat, 6 Sep 2014 22:50:06 +0900 Subject: implement Hash.[] --- mrbgems/mruby-hash-ext/mrblib/hash.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'mrbgems/mruby-hash-ext/mrblib') diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 9da08dc3a..6546af644 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -3,6 +3,39 @@ class Hash # ISO does not define Hash#each_pair, so each_pair is defined in gem. alias each_pair each + def self.[](*object) + o = object[0] + if o.respond_to?(:to_hash) + h = Hash.new + object[0].to_hash.each { |k, v| h[k] = v } + return h + elsif o.respond_to?(:to_a) + h = Hash.new + o.to_a.each do |i| + raise ArgumentError, "wrong element type #{i.class} at #{__LINE__} (expected array)" unless i.respond_to?(:to_a) + k, v = nil + case i.size + when 2 + k = i[0] + v = i[1] + when 1 + k = i[0] + else + raise ArgumentError, "invalid number of elements (#{i.size} for 1..2)" + end + h[k] = v + end + return h + end + raise ArgumentError, 'odd number of arguments for Hash' unless object.length % 2 == 0 + h = Hash.new + t = (0...(object.length >> 1)).map { |i| i * 2 } + for i in t do + h[object[i]] = object[i + 1] + end + h + end + ## # call-seq: # hsh.merge!(other_hash) -> hsh -- cgit v1.2.3 From e66d86a5fb8ab79403ab47423f9a871fdc5b26fb Mon Sep 17 00:00:00 2001 From: INOUE Yasuyuki Date: Thu, 18 Sep 2014 16:07:27 +0900 Subject: refactor Hash generator loop --- mrbgems/mruby-hash-ext/mrblib/hash.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'mrbgems/mruby-hash-ext/mrblib') diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 6546af644..891aae27d 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -29,8 +29,7 @@ class Hash end raise ArgumentError, 'odd number of arguments for Hash' unless object.length % 2 == 0 h = Hash.new - t = (0...(object.length >> 1)).map { |i| i * 2 } - for i in t do + 0.step(object.length - 2, 2) do |i| h[object[i]] = object[i + 1] end h -- cgit v1.2.3 From d30aadf30626a16f8c83359fbb2205ee1e5fd464 Mon Sep 17 00:00:00 2001 From: INOUE Yasuyuki Date: Thu, 18 Sep 2014 17:37:48 +0900 Subject: remove line number from raise message --- mrbgems/mruby-hash-ext/mrblib/hash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-hash-ext/mrblib') diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 891aae27d..82847c39b 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -12,7 +12,7 @@ class Hash elsif o.respond_to?(:to_a) h = Hash.new o.to_a.each do |i| - raise ArgumentError, "wrong element type #{i.class} at #{__LINE__} (expected array)" unless i.respond_to?(:to_a) + raise ArgumentError, "wrong element type #{i.class} (expected array)" unless i.respond_to?(:to_a) k, v = nil case i.size when 2 -- cgit v1.2.3 From 5f16c41753f7789065f2c2bfe84395cad8cadcaa Mon Sep 17 00:00:00 2001 From: INOUE Yasuyuki Date: Thu, 18 Sep 2014 17:43:38 +0900 Subject: copy documentation comment from CRuby --- mrbgems/mruby-hash-ext/mrblib/hash.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'mrbgems/mruby-hash-ext/mrblib') diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 82847c39b..f214cb1d5 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -3,6 +3,26 @@ class Hash # ISO does not define Hash#each_pair, so each_pair is defined in gem. alias each_pair each + ## + # call-seq: + # Hash[ key, value, ... ] -> new_hash + # Hash[ [ [key, value], ... ] ] -> new_hash + # Hash[ object ] -> new_hash + # + # Creates a new hash populated with the given objects. + # + # Similar to the literal { _key_ => _value_, ... }. In the first + # form, keys and values occur in pairs, so there must be an even number of + # arguments. + # + # The second and third form take a single argument which is either an array + # of key-value pairs or an object convertible to a hash. + # + # Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200} + # Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200} + # Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200} + # + def self.[](*object) o = object[0] if o.respond_to?(:to_hash) -- cgit v1.2.3