summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-hash-ext/mrblib/hash.rb
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2018-09-20 10:13:36 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2018-09-20 10:13:36 +0900
commit8e91316bd64aa39b3afeffb0ade5eb38b05d3953 (patch)
treefabce96f895417c6d2252860211aeb7b4df97a31 /mrbgems/mruby-hash-ext/mrblib/hash.rb
parent3820ef791f3ccb766956d12534ae519f4923ac7e (diff)
parentcb85fa6787ea9467f81be41570a36b475b7ef061 (diff)
downloadmruby-8e91316bd64aa39b3afeffb0ade5eb38b05d3953.tar.gz
mruby-8e91316bd64aa39b3afeffb0ade5eb38b05d3953.zip
Merge branch 'no-implicit-conversion' into mruby2-draftmruby2-draft
Diffstat (limited to 'mrbgems/mruby-hash-ext/mrblib/hash.rb')
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb49
1 files changed, 7 insertions, 42 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb
index 61e4c890c..3c2d99836 100644
--- a/mrbgems/mruby-hash-ext/mrblib/hash.rb
+++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb
@@ -27,9 +27,9 @@ class Hash
length = object.length
if length == 1
o = object[0]
- if o.respond_to?(:to_hash)
+ if Hash === o
h = self.new
- object[0].to_hash.each { |k, v| h[k] = v }
+ o.each { |k, v| h[k] = v }
return h
elsif o.respond_to?(:to_a)
h = self.new
@@ -62,25 +62,6 @@ class Hash
##
# call-seq:
- # Hash.try_convert(obj) -> hash or nil
- #
- # Try to convert <i>obj</i> into a hash, using to_hash method.
- # Returns converted hash or nil if <i>obj</i> cannot be converted
- # for any reason.
- #
- # Hash.try_convert({1=>2}) # => {1=>2}
- # Hash.try_convert("1=>2") # => nil
- #
- def self.try_convert(obj)
- if obj.respond_to?(:to_hash)
- obj.to_hash
- else
- nil
- end
- end
-
- ##
- # call-seq:
# hsh.merge!(other_hash) -> hsh
# hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh
#
@@ -101,7 +82,7 @@ class Hash
#
def merge!(other, &block)
- raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
+ raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
if block
other.each_key{|k|
self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
@@ -329,11 +310,7 @@ class Hash
# h1 < h1 #=> false
#
def <(hash)
- begin
- hash = hash.to_hash
- rescue NoMethodError
- raise TypeError, "can't convert #{hash.class} to Hash"
- end
+ raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
size < hash.size and all? {|key, val|
hash.key?(key) and hash[key] == val
}
@@ -353,11 +330,7 @@ class Hash
# h1 <= h1 #=> true
#
def <=(hash)
- begin
- hash = hash.to_hash
- rescue NoMethodError
- raise TypeError, "can't convert #{hash.class} to Hash"
- end
+ raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
size <= hash.size and all? {|key, val|
hash.key?(key) and hash[key] == val
}
@@ -377,11 +350,7 @@ class Hash
# h1 > h1 #=> false
#
def >(hash)
- begin
- hash = hash.to_hash
- rescue NoMethodError
- raise TypeError, "can't convert #{hash.class} to Hash"
- end
+ raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
size > hash.size and hash.all? {|key, val|
key?(key) and self[key] == val
}
@@ -401,11 +370,7 @@ class Hash
# h1 >= h1 #=> true
#
def >=(hash)
- begin
- hash = hash.to_hash
- rescue NoMethodError
- raise TypeError, "can't convert #{hash.class} to Hash"
- end
+ raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
size >= hash.size and hash.all? {|key, val|
key?(key) and self[key] == val
}