summaryrefslogtreecommitdiffhomepage
path: root/mrbgems
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2018-09-19 22:53:48 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2018-09-19 22:53:48 +0900
commitcb85fa6787ea9467f81be41570a36b475b7ef061 (patch)
tree0f75fd9dbe38a0700dc47963d0900aaafc5ea031 /mrbgems
parentb4a35344a67812510a34f93f9d6b8400a2183432 (diff)
downloadmruby-cb85fa6787ea9467f81be41570a36b475b7ef061.tar.gz
mruby-cb85fa6787ea9467f81be41570a36b475b7ef061.zip
Removed `to_hash` conversion method.
Diffstat (limited to 'mrbgems')
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb30
-rw-r--r--mrbgems/mruby-kernel-ext/src/kernel.c20
-rw-r--r--mrbgems/mruby-sprintf/src/sprintf.c2
3 files changed, 14 insertions, 38 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb
index a839ff4a6..604780c46 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
@@ -82,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]
@@ -310,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
}
@@ -334,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
}
@@ -358,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
}
@@ -382,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
}
diff --git a/mrbgems/mruby-kernel-ext/src/kernel.c b/mrbgems/mruby-kernel-ext/src/kernel.c
index 324753f6e..99affbfa4 100644
--- a/mrbgems/mruby-kernel-ext/src/kernel.c
+++ b/mrbgems/mruby-kernel-ext/src/kernel.c
@@ -184,9 +184,9 @@ mrb_f_array(mrb_state *mrb, mrb_value self)
* call-seq:
* Hash(arg) -> hash
*
- * Converts <i>arg</i> to a <code>Hash</code> by calling
- * <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when
- * <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
+ * Returns a <code>Hash</code> if <i>arg</i> is a <code>Hash</code>.
+ * Returns an empty <code>Hash</code> when <i>arg</i> is <tt>nil</tt>
+ * or <tt>[]</tt>.
*
* Hash([]) #=> {}
* Hash(nil) #=> {}
@@ -197,21 +197,13 @@ mrb_f_array(mrb_state *mrb, mrb_value self)
static mrb_value
mrb_f_hash(mrb_state *mrb, mrb_value self)
{
- mrb_value arg, tmp;
+ mrb_value arg;
mrb_get_args(mrb, "o", &arg);
- if (mrb_nil_p(arg)) {
+ if (mrb_nil_p(arg) || (mrb_array_p(arg) && RARRAY_LEN(arg) == 0)) {
return mrb_hash_new(mrb);
}
- tmp = mrb_check_convert_type(mrb, arg, MRB_TT_HASH, "Hash", "to_hash");
- if (mrb_nil_p(tmp)) {
- if (mrb_array_p(arg) && RARRAY_LEN(arg) == 0) {
- return mrb_hash_new(mrb);
- }
- mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into Hash",
- mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, arg)));
- }
- return tmp;
+ return mrb_ensure_hash_type(mrb, arg);
}
/*
diff --git a/mrbgems/mruby-sprintf/src/sprintf.c b/mrbgems/mruby-sprintf/src/sprintf.c
index 15d7b5464..5a4a7899e 100644
--- a/mrbgems/mruby-sprintf/src/sprintf.c
+++ b/mrbgems/mruby-sprintf/src/sprintf.c
@@ -233,7 +233,7 @@ get_hash(mrb_state *mrb, mrb_value *hash, mrb_int argc, const mrb_value *argv)
if (argc != 2) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "one hash required");
}
- tmp = mrb_check_convert_type(mrb, argv[1], MRB_TT_HASH, "Hash", "to_hash");
+ tmp = mrb_check_hash_type(mrb, argv[1]);
if (mrb_nil_p(tmp)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "one hash required");
}