summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/mruby.h6
-rw-r--r--include/mruby/array.h1
-rw-r--r--include/mruby/string.h3
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb36
-rw-r--r--mrbgems/mruby-array-ext/test/array.rb23
-rw-r--r--mrbgems/mruby-enum-ext/mrblib/enum.rb27
-rw-r--r--mrbgems/mruby-enumerator/mrblib/enumerator.rb12
-rw-r--r--mrbgems/mruby-enumerator/test/enumerator.rb6
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb49
-rw-r--r--mrbgems/mruby-hash-ext/test/hash.rb6
-rw-r--r--mrbgems/mruby-io/src/file.c2
-rw-r--r--mrbgems/mruby-kernel-ext/src/kernel.c42
-rw-r--r--mrbgems/mruby-numeric-ext/src/numeric_ext.c17
-rw-r--r--mrbgems/mruby-pack/src/pack.c6
-rw-r--r--mrbgems/mruby-random/src/random.c12
-rw-r--r--mrbgems/mruby-random/test/random.rb12
-rw-r--r--mrbgems/mruby-range-ext/mrblib/range.rb5
-rw-r--r--mrbgems/mruby-sprintf/src/sprintf.c2
-rw-r--r--mrbgems/mruby-string-ext/mrblib/string.rb21
-rw-r--r--mrbgems/mruby-string-ext/src/string.c6
-rw-r--r--mrbgems/mruby-string-ext/test/string.rb30
-rw-r--r--mrblib/array.rb2
-rw-r--r--mrblib/hash.rb14
-rw-r--r--mrblib/string.rb11
-rw-r--r--src/array.c27
-rw-r--r--src/class.c27
-rw-r--r--src/hash.c28
-rw-r--r--src/kernel.c41
-rw-r--r--src/numeric.c4
-rw-r--r--src/object.c118
-rw-r--r--src/string.c32
-rw-r--r--test/t/string.rb13
32 files changed, 174 insertions, 467 deletions
diff --git a/include/mruby.h b/include/mruby.h
index 2d30d3d27..ab5d5b167 100644
--- a/include/mruby.h
+++ b/include/mruby.h
@@ -716,7 +716,6 @@ MRB_API mrb_value mrb_notimplement_m(mrb_state*, mrb_value);
* @return [mrb_value] The newly duplicated object.
*/
MRB_API mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
-MRB_API mrb_value mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method);
/**
* Returns true if obj responds to the given method. If the method was defined for that
@@ -855,10 +854,6 @@ typedef const char *mrb_args_format;
/**
* Retrieve arguments from mrb_state.
*
- * When applicable, implicit conversions (such as `to_str`, `to_ary`, `to_hash`) are
- * applied to received arguments.
- * Used inside a function of mrb_func_t type.
- *
* @param mrb The current MRuby state.
* @param format [mrb_args_format] is a list of format specifiers
* @param ... The passing variadic arguments must be a pointer of retrieving type.
@@ -1188,6 +1183,7 @@ MRB_API void mrb_gc_unregister(mrb_state *mrb, mrb_value obj);
MRB_API mrb_value mrb_to_int(mrb_state *mrb, mrb_value val);
#define mrb_int(mrb, val) mrb_fixnum(mrb_to_int(mrb, val))
+MRB_API mrb_value mrb_to_str(mrb_state *mrb, mrb_value val);
MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
typedef enum call_type {
diff --git a/include/mruby/array.h b/include/mruby/array.h
index 6fffe4512..2457f68f2 100644
--- a/include/mruby/array.h
+++ b/include/mruby/array.h
@@ -199,6 +199,7 @@ MRB_API void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val
* @param other The array to replace it with.
*/
MRB_API void mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other);
+MRB_API mrb_value mrb_ensure_array_type(mrb_state *mrb, mrb_value self);
MRB_API mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self);
/*
diff --git a/include/mruby/string.h b/include/mruby/string.h
index 481b2fb38..fa1955f48 100644
--- a/include/mruby/string.h
+++ b/include/mruby/string.h
@@ -311,8 +311,7 @@ MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb
* @param [mrb_value] str Ruby string.
* @return [mrb_value] A Ruby string.
*/
-MRB_API mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
-
+MRB_API mrb_value mrb_ensure_string_type(mrb_state *mrb, mrb_value str);
MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
MRB_API mrb_value mrb_str_new_capa(mrb_state *mrb, size_t capa);
MRB_API mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa);
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index c0995bb99..f94d08765 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -1,31 +1,7 @@
+# coding: cp932
class Array
##
# call-seq:
- # Array.try_convert(obj) -> array or nil
- #
- # Tries to convert +obj+ into an array, using +to_ary+ method.
- # converted array or +nil+ if +obj+ cannot be converted for any reason.
- # This method can be used to check if an argument is an array.
- #
- # Array.try_convert([1]) #=> [1]
- # Array.try_convert("1") #=> nil
- #
- # if tmp = Array.try_convert(arg)
- # # the argument is an array
- # elsif tmp = String.try_convert(arg)
- # # the argument is a string
- # end
- #
- def self.try_convert(obj)
- if obj.respond_to?(:to_ary)
- obj.to_ary
- else
- nil
- end
- end
-
- ##
- # call-seq:
# ary.uniq! -> ary or nil
# ary.uniq! { |item| ... } -> ary or nil
#
@@ -783,16 +759,6 @@ class Array
end
##
- # call-seq:
- # ary.to_ary -> ary
- #
- # Returns +self+.
- #
- def to_ary
- self
- end
-
- ##
# call-seq:
# ary.dig(idx, ...) -> object
#
diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb
index 4f54c65c3..d0b5bec6d 100644
--- a/mrbgems/mruby-array-ext/test/array.rb
+++ b/mrbgems/mruby-array-ext/test/array.rb
@@ -1,13 +1,6 @@
##
# Array(Ext) Test
-assert("Array.try_convert") do
- assert_nil Array.try_convert(0)
- assert_nil Array.try_convert(nil)
- assert_equal [], Array.try_convert([])
- assert_equal [1,2,3], Array.try_convert([1,2,3])
-end
-
assert("Array#assoc") do
s1 = [ "colors", "red", "blue", "green" ]
s2 = [ "letters", "a", "b", "c" ]
@@ -330,27 +323,11 @@ assert('Array#to_h') do
assert_raise(ArgumentError) { [[1]].to_h }
end
-assert('Array#to_h (Modified)') do
- class A
- def to_ary
- $a.clear
- nil
- end
- end
- $a = [A.new]
- assert_raise(TypeError) { $a.to_h }
-end
-
assert("Array#index (block)") do
assert_nil (1..10).to_a.index { |i| i % 5 == 0 and i % 7 == 0 }
assert_equal 34, (1..100).to_a.index { |i| i % 5 == 0 and i % 7 == 0 }
end
-assert("Array#to_ary") do
- assert_equal [], [].to_ary
- assert_equal [1,2,3], [1,2,3].to_ary
-end
-
assert("Array#dig") do
h = [[[1]], 0]
assert_equal(1, h.dig(0, 0, 0))
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb
index a840ade3b..e33ce2c5a 100644
--- a/mrbgems/mruby-enum-ext/mrblib/enum.rb
+++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb
@@ -13,10 +13,9 @@ module Enumerable
# a.drop(3) #=> [4, 5, 0]
def drop(n)
- raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
+ n = n.__to_int
raise ArgumentError, "attempt to drop negative size" if n < 0
- n = n.to_int
ary = []
self.each {|*val| n == 0 ? ary << val.__svalue : n -= 1 }
ary
@@ -57,8 +56,8 @@ module Enumerable
# a.take(3) #=> [1, 2, 3]
def take(n)
- raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
- i = n.to_int
+ n = n.__to_int
+ i = n.to_i
raise ArgumentError, "attempt to take negative size" if i < 0
ary = []
return ary if i == 0
@@ -113,12 +112,12 @@ module Enumerable
# [8, 9, 10]
def each_cons(n, &block)
- raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
+ n = n.__to_int
raise ArgumentError, "invalid size" if n <= 0
return to_enum(:each_cons,n) unless block
ary = []
- n = n.to_int
+ n = n.to_i
self.each do |*val|
ary.shift if ary.size == n
ary << val.__svalue
@@ -141,12 +140,12 @@ module Enumerable
# [10]
def each_slice(n, &block)
- raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
+ n = n.__to_int
raise ArgumentError, "invalid slice size" if n <= 0
return to_enum(:each_slice,n) unless block
ary = []
- n = n.to_int
+ n = n.to_i
self.each do |*val|
ary << val.__svalue
if ary.size == n
@@ -225,9 +224,7 @@ module Enumerable
end
return nil
when 1
- n = args[0]
- raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
- i = n.to_int
+ i = args[0].__to_int
raise ArgumentError, "attempt to take negative size" if i < 0
ary = []
return ary if i == 0
@@ -675,13 +672,7 @@ module Enumerable
if nv.nil?
n = -1
else
- unless nv.respond_to?(:to_int)
- raise TypeError, "no implicit conversion of #{nv.class} into Integer"
- end
- n = nv.to_int
- unless n.kind_of?(Integer)
- raise TypeError, "no implicit conversion of #{nv.class} into Integer"
- end
+ n = nv.__to_int
return nil if n <= 0
end
diff --git a/mrbgems/mruby-enumerator/mrblib/enumerator.rb b/mrbgems/mruby-enumerator/mrblib/enumerator.rb
index 7ca1d5eb6..ae692dd12 100644
--- a/mrbgems/mruby-enumerator/mrblib/enumerator.rb
+++ b/mrbgems/mruby-enumerator/mrblib/enumerator.rb
@@ -157,12 +157,10 @@ class Enumerator
def with_index(offset=0, &block)
return to_enum :with_index, offset unless block
- offset = if offset.nil?
- 0
- elsif offset.respond_to?(:to_int)
- offset.to_int
+ if offset.nil?
+ offset = 0
else
- raise TypeError, "no implicit conversion of #{offset.class} into Integer"
+ offset = offset.__to_int
end
n = offset - 1
@@ -623,9 +621,7 @@ module Enumerable
# use Enumerator to use infinite sequence
def zip(*args, &block)
args = args.map do |a|
- if a.respond_to?(:to_ary)
- a.to_ary.to_enum(:each)
- elsif a.respond_to?(:each)
+ if a.respond_to?(:each)
a.to_enum(:each)
else
raise TypeError, "wrong argument type #{a.class} (must respond to :each)"
diff --git a/mrbgems/mruby-enumerator/test/enumerator.rb b/mrbgems/mruby-enumerator/test/enumerator.rb
index 428ea0307..c24b70ccb 100644
--- a/mrbgems/mruby-enumerator/test/enumerator.rb
+++ b/mrbgems/mruby-enumerator/test/enumerator.rb
@@ -55,12 +55,6 @@ assert 'Enumerator#with_index' do
assert_equal [[[1, 10], 20], [[2, 11], 21], [[3, 12], 22]], a
end
-assert 'Enumerator#with_index nonnum offset' do
- s = Object.new
- def s.to_int; 1 end
- assert_equal([[1,1],[2,2],[3,3]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a)
-end
-
assert 'Enumerator#with_index string offset' do
assert_raise(TypeError){ @obj.to_enum(:foo, 1, 2, 3).with_index('1').to_a }
end
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
}
diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb
index 269da800d..b5d0aaaf8 100644
--- a/mrbgems/mruby-hash-ext/test/hash.rb
+++ b/mrbgems/mruby-hash-ext/test/hash.rb
@@ -45,12 +45,6 @@ assert('Hash.[] for sub class') do
assert_equal(sub_hash_class, sub_hash.class)
end
-assert('Hash.try_convert') do
- assert_nil Hash.try_convert(nil)
- assert_nil Hash.try_convert("{1=>2}")
- assert_equal({1=>2}, Hash.try_convert({1=>2}))
-end
-
assert('Hash#merge!') do
a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
b = { 'cba_key' => 'XXX', 'xyz_key' => 'xyz_value' }
diff --git a/mrbgems/mruby-io/src/file.c b/mrbgems/mruby-io/src/file.c
index e65741061..c00663481 100644
--- a/mrbgems/mruby-io/src/file.c
+++ b/mrbgems/mruby-io/src/file.c
@@ -115,7 +115,7 @@ mrb_file_s_unlink(mrb_state *mrb, mrb_value obj)
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
const char *utf8_path;
- pathv = mrb_convert_type(mrb, argv[i], MRB_TT_STRING, "String", "to_str");
+ pathv = mrb_ensure_string_type(mrb, argv[i]);
utf8_path = mrb_string_value_cstr(mrb, &pathv);
path = mrb_locale_from_utf8(utf8_path, -1);
if (UNLINK(path) < 0) {
diff --git a/mrbgems/mruby-kernel-ext/src/kernel.c b/mrbgems/mruby-kernel-ext/src/kernel.c
index 32d86376a..99affbfa4 100644
--- a/mrbgems/mruby-kernel-ext/src/kernel.c
+++ b/mrbgems/mruby-kernel-ext/src/kernel.c
@@ -93,9 +93,8 @@ mrb_f_method(mrb_state *mrb, mrb_value self)
* (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
* In any case, strings should be strictly conformed to numeric
* representation. This behavior is different from that of
- * <code>String#to_i</code>. Non string values will be converted using
- * <code>to_int</code>, and <code>to_i</code>. Passing <code>nil</code>
- * raises a TypeError.
+ * <code>String#to_i</code>. Non string values will be treated as integers.
+ * Passing <code>nil</code> raises a TypeError.
*
* Integer(123.999) #=> 123
* Integer("0x1a") #=> 26
@@ -142,8 +141,7 @@ mrb_f_float(mrb_state *mrb, mrb_value self)
* String(arg) -> string
*
* Returns <i>arg</i> as an <code>String</code>.
- *
- * First tries to call its <code>to_str</code> method, then its to_s method.
+ * converted using <code>to_s</code> method.
*
* String(self) #=> "main"
* String(self.class) #=> "Object"
@@ -155,10 +153,7 @@ mrb_f_string(mrb_state *mrb, mrb_value self)
mrb_value arg, tmp;
mrb_get_args(mrb, "o", &arg);
- tmp = mrb_check_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_str");
- if (mrb_nil_p(tmp)) {
- tmp = mrb_check_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_s");
- }
+ tmp = mrb_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_s");
return tmp;
}
@@ -166,9 +161,7 @@ mrb_f_string(mrb_state *mrb, mrb_value self)
* call-seq:
* Array(arg) -> array
*
- * Returns +arg+ as an Array.
- *
- * First tries to call Array#to_ary on +arg+, then Array#to_a.
+ * Returns +arg+ as an Array using to_a method.
*
* Array(1..5) #=> [1, 2, 3, 4, 5]
*
@@ -179,10 +172,7 @@ mrb_f_array(mrb_state *mrb, mrb_value self)
mrb_value arg, tmp;
mrb_get_args(mrb, "o", &arg);
- tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_ary");
- if (mrb_nil_p(tmp)) {
- tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_a");
- }
+ tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_a");
if (mrb_nil_p(tmp)) {
return mrb_ary_new_from_values(mrb, 1, &arg);
}
@@ -194,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) #=> {}
@@ -207,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-numeric-ext/src/numeric_ext.c b/mrbgems/mruby-numeric-ext/src/numeric_ext.c
index 1d6a07769..cd8bbf187 100644
--- a/mrbgems/mruby-numeric-ext/src/numeric_ext.c
+++ b/mrbgems/mruby-numeric-ext/src/numeric_ext.c
@@ -2,13 +2,10 @@
#include <mruby.h>
static inline mrb_int
-to_int(mrb_value x)
+to_int(mrb_state *mrb, mrb_value x)
{
- double f;
-
- if (mrb_fixnum_p(x)) return mrb_fixnum(x);
- f = mrb_float(x);
- return (mrb_int)f;
+ x = mrb_to_int(mrb, x);
+ return mrb_fixnum(x);
}
/*
@@ -28,7 +25,7 @@ mrb_int_chr(mrb_state *mrb, mrb_value x)
mrb_int chr;
char c;
- chr = to_int(x);
+ chr = to_int(mrb, x);
if (chr >= (1 << CHAR_BIT)) {
mrb_raisef(mrb, E_RANGE_ERROR, "%S out of char range", x);
}
@@ -48,8 +45,8 @@ mrb_int_allbits(mrb_state *mrb, mrb_value self)
{
mrb_int n, m;
- n = to_int(self);
mrb_get_args(mrb, "i", &m);
+ n = to_int(mrb, self);
return mrb_bool_value((n & m) == m);
}
@@ -64,8 +61,8 @@ mrb_int_anybits(mrb_state *mrb, mrb_value self)
{
mrb_int n, m;
- n = to_int(self);
mrb_get_args(mrb, "i", &m);
+ n = to_int(mrb, self);
return mrb_bool_value((n & m) != 0);
}
@@ -80,8 +77,8 @@ mrb_int_nobits(mrb_state *mrb, mrb_value self)
{
mrb_int n, m;
- n = to_int(self);
mrb_get_args(mrb, "i", &m);
+ n = to_int(mrb, self);
return mrb_bool_value((n & m) == 0);
}
diff --git a/mrbgems/mruby-pack/src/pack.c b/mrbgems/mruby-pack/src/pack.c
index f970d9339..0fa8c90bd 100644
--- a/mrbgems/mruby-pack/src/pack.c
+++ b/mrbgems/mruby-pack/src/pack.c
@@ -1124,14 +1124,16 @@ mrb_pack_pack(mrb_state *mrb, mrb_value ary)
o = mrb_ary_ref(mrb, ary, aidx);
if (type == PACK_TYPE_INTEGER) {
o = mrb_to_int(mrb, o);
+ }
#ifndef MRB_WITHOUT_FLOAT
- } else if (type == PACK_TYPE_FLOAT) {
+ else if (type == PACK_TYPE_FLOAT) {
if (!mrb_float_p(o)) {
mrb_float f = mrb_to_flo(mrb, o);
o = mrb_float_value(mrb, f);
}
+ }
#endif
- } else if (type == PACK_TYPE_STRING) {
+ else if (type == PACK_TYPE_STRING) {
if (!mrb_string_p(o)) {
mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into String", mrb_class_path(mrb, mrb_obj_class(mrb, o)));
}
diff --git a/mrbgems/mruby-random/src/random.c b/mrbgems/mruby-random/src/random.c
index 5b926a228..68209840a 100644
--- a/mrbgems/mruby-random/src/random.c
+++ b/mrbgems/mruby-random/src/random.c
@@ -79,12 +79,12 @@ get_opt(mrb_state* mrb)
mrb_get_args(mrb, "|o", &arg);
if (!mrb_nil_p(arg)) {
- arg = mrb_check_convert_type(mrb, arg, MRB_TT_FIXNUM, "Fixnum", "to_int");
- if (mrb_nil_p(arg)) {
- mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument type");
- }
- if (mrb_fixnum(arg) < 0) {
- arg = mrb_fixnum_value(0 - mrb_fixnum(arg));
+ mrb_int i;
+
+ arg = mrb_to_int(mrb, arg);
+ i = mrb_fixnum(arg);
+ if (i < 0) {
+ arg = mrb_fixnum_value(0 - i);
}
}
return arg;
diff --git a/mrbgems/mruby-random/test/random.rb b/mrbgems/mruby-random/test/random.rb
index 1c59be3a6..1653ae4a6 100644
--- a/mrbgems/mruby-random/test/random.rb
+++ b/mrbgems/mruby-random/test/random.rb
@@ -74,15 +74,3 @@ assert('Array#shuffle!(random)') do
ary1 != [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and 10.times { |x| ary1.include? x } and ary1 == ary2
end
-
-assert('Array#sample checks input length after reading arguments') do
- $ary = [1, 2, 3]
- class ArrayChange
- def to_i
- $ary << 4
- 4
- end
- end
-
- assert_equal [1, 2, 3, 4], $ary.sample(ArrayChange.new).sort
-end
diff --git a/mrbgems/mruby-range-ext/mrblib/range.rb b/mrbgems/mruby-range-ext/mrblib/range.rb
index e5d1fb079..de7925ba7 100644
--- a/mrbgems/mruby-range-ext/mrblib/range.rb
+++ b/mrbgems/mruby-range-ext/mrblib/range.rb
@@ -15,10 +15,7 @@ class Range
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1)" unless args.length == 1
nv = args[0]
- raise TypeError, "no implicit conversion from nil to integer" if nv.nil?
- raise TypeError, "no implicit conversion of #{nv.class} into Integer" unless nv.respond_to?(:to_int)
- n = nv.to_int
- raise TypeError, "no implicit conversion of #{nv.class} into Integer" unless n.kind_of?(Integer)
+ n = nv.__to_int
raise ArgumentError, "negative array size (or size too big)" unless 0 <= n
ary = []
each do |i|
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");
}
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb
index 27ca30610..2a323c858 100644
--- a/mrbgems/mruby-string-ext/mrblib/string.rb
+++ b/mrbgems/mruby-string-ext/mrblib/string.rb
@@ -1,25 +1,6 @@
class String
##
- # call-seq:
- # String.try_convert(obj) -> string or nil
- #
- # Try to convert <i>obj</i> into a String, using to_str method.
- # Returns converted string or nil if <i>obj</i> cannot be converted
- # for any reason.
- #
- # String.try_convert("str") #=> "str"
- # String.try_convert(/re/) #=> nil
- #
- def self.try_convert(obj)
- if obj.respond_to?(:to_str)
- obj.to_str
- else
- nil
- end
- end
-
- ##
# call-seq:
# string.clear -> string
#
@@ -142,7 +123,7 @@ class String
# "abcdef".casecmp("ABCDEF") #=> 0
#
def casecmp(str)
- self.downcase <=> str.to_str.downcase
+ self.downcase <=> str.__to_str.downcase
rescue NoMethodError
nil
end
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c
index 142d449f4..7f48efe48 100644
--- a/mrbgems/mruby-string-ext/src/string.c
+++ b/mrbgems/mruby-string-ext/src/string.c
@@ -163,7 +163,7 @@ mrb_str_concat_m(mrb_state *mrb, mrb_value self)
if (mrb_fixnum_p(str))
str = mrb_fixnum_chr(mrb, str);
else
- str = mrb_string_type(mrb, str);
+ str = mrb_ensure_string_type(mrb, str);
mrb_str_concat(mrb, self, str);
return self;
}
@@ -191,7 +191,7 @@ mrb_str_start_with(mrb_state *mrb, mrb_value self)
for (i = 0; i < argc; i++) {
size_t len_l, len_r;
int ai = mrb_gc_arena_save(mrb);
- sub = mrb_string_type(mrb, argv[i]);
+ sub = mrb_ensure_string_type(mrb, argv[i]);
mrb_gc_arena_restore(mrb, ai);
len_l = RSTRING_LEN(self);
len_r = RSTRING_LEN(sub);
@@ -220,7 +220,7 @@ mrb_str_end_with(mrb_state *mrb, mrb_value self)
for (i = 0; i < argc; i++) {
size_t len_l, len_r;
int ai = mrb_gc_arena_save(mrb);
- sub = mrb_string_type(mrb, argv[i]);
+ sub = mrb_ensure_string_type(mrb, argv[i]);
mrb_gc_arena_restore(mrb, ai);
len_l = RSTRING_LEN(self);
len_r = RSTRING_LEN(sub);
diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb
index b6146fb90..b44e8181a 100644
--- a/mrbgems/mruby-string-ext/test/string.rb
+++ b/mrbgems/mruby-string-ext/test/string.rb
@@ -4,13 +4,6 @@
UTF8STRING = ("\343\201\202".size == 1)
-assert('String.try_convert') do
- assert_nil String.try_convert(nil)
- assert_nil String.try_convert(:foo)
- assert_equal "", String.try_convert("")
- assert_equal "1,2,3", String.try_convert("1,2,3")
-end
-
assert('String#getbyte') do
str1 = "hello"
bytes1 = [104, 101, 108, 108, 111]
@@ -31,18 +24,6 @@ assert('String#setbyte') do
assert_equal("Hello", str1)
end
-assert("String#setbyte raises IndexError if arg conversion resizes String") do
- $s = "01234\n"
- class Tmp
- def to_i
- $s.chomp! ''
- 95
- end
- end
- tmp = Tmp.new
- assert_raise(IndexError) { $s.setbyte(5, tmp) }
-end
-
assert('String#byteslice') do
str1 = "hello"
assert_equal("e", str1.byteslice(1))
@@ -126,12 +107,6 @@ assert('String#concat') do
assert_equal "Hello World!", "Hello " << "World" << 33
assert_equal "Hello World!", "Hello ".concat("World").concat(33)
- o = Object.new
- def o.to_str
- "to_str"
- end
- assert_equal "hi to_str", "hi " << o
-
assert_raise(TypeError) { "".concat(Object.new) }
end
@@ -140,11 +115,6 @@ assert('String#casecmp') do
assert_equal 0, "aBcDeF".casecmp("abcdef")
assert_equal(-1, "abcdef".casecmp("abcdefg"))
assert_equal 0, "abcdef".casecmp("ABCDEF")
- o = Object.new
- def o.to_str
- "ABCDEF"
- end
- assert_equal 0, "abcdef".casecmp(o)
end
assert('String#start_with?') do
diff --git a/mrblib/array.rb b/mrblib/array.rb
index 13c5d646c..16c602ba0 100644
--- a/mrblib/array.rb
+++ b/mrblib/array.rb
@@ -66,7 +66,7 @@ class Array
#
# ISO 15.2.12.5.15
def initialize(size=0, obj=nil, &block)
- raise TypeError, "expected Integer for 1st argument" unless size.kind_of? Integral
+ size = size.__to_int
raise ArgumentError, "negative array size" if size < 0
self.clear
diff --git a/mrblib/hash.rb b/mrblib/hash.rb
index 96029a230..1cd039c45 100644
--- a/mrblib/hash.rb
+++ b/mrblib/hash.rb
@@ -12,9 +12,7 @@ class Hash
# ISO 15.2.13.4.1
def ==(hash)
return true if self.equal?(hash)
- begin
- hash = hash.to_hash
- rescue NoMethodError
+ unless Hash === hash
return false
end
return false if self.size != hash.size
@@ -32,9 +30,7 @@ class Hash
# ISO 15.2.13.4.32 (x)
def eql?(hash)
return true if self.equal?(hash)
- begin
- hash = hash.to_hash
- rescue NoMethodError
+ unless Hash === hash
return false
end
return false if self.size != hash.size
@@ -154,9 +150,8 @@ class Hash
#
# ISO 15.2.13.4.23
def replace(hash)
- raise TypeError, "can't convert argument into Hash" unless hash.respond_to?(:to_hash)
+ raise TypeError, "Hash required (#{hash.class} given)" unless Hash === hash
self.clear
- hash = hash.to_hash
hash.each_key{|k|
self[k] = hash[k]
}
@@ -179,8 +174,7 @@ class Hash
#
# ISO 15.2.13.4.22
def merge(other, &block)
- raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
- other = other.to_hash
+ raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
h = self.dup
if block
other.each_key{|k|
diff --git a/mrblib/string.rb b/mrblib/string.rb
index 07b80b340..397603e9d 100644
--- a/mrblib/string.rb
+++ b/mrblib/string.rb
@@ -12,7 +12,7 @@ class String
def each_line(rs = "\n", &block)
return to_enum(:each_line, rs, &block) unless block
return block.call(self) if rs.nil?
- rs = rs.to_str
+ rs = rs.__to_str
offset = 0
rs_len = rs.length
this = dup
@@ -67,7 +67,7 @@ class String
block = nil
end
if !replace.nil? || !block
- replace = replace.to_str
+ replace = replace.__to_str
end
offset = 0
result = []
@@ -129,12 +129,12 @@ class String
end
pattern, replace = *args
- pattern = pattern.to_str
+ pattern = pattern.__to_str
if args.length == 2 && block
block = nil
end
unless block
- replace = replace.to_str
+ replace = replace.__to_str
end
result = []
this = dup
@@ -245,14 +245,13 @@ class String
##
# ISO 15.2.10.5.3
def =~(re)
- raise TypeError, "type mismatch: String given" if re.respond_to? :to_str
re =~ self
end
##
# ISO 15.2.10.5.27
def match(re, &block)
- if re.respond_to? :to_str
+ if String === re
if Object.const_defined?(:Regexp)
r = Regexp.new(re)
r.match(self, &block)
diff --git a/src/array.c b/src/array.c
index 0b039a6ec..32ee3434e 100644
--- a/src/array.c
+++ b/src/array.c
@@ -1058,7 +1058,7 @@ mrb_ary_rindex_m(mrb_state *mrb, mrb_value self)
MRB_API mrb_value
mrb_ary_splat(mrb_state *mrb, mrb_value v)
{
- mrb_value a, recv_class;
+ mrb_value a;
if (mrb_array_p(v)) {
return v;
@@ -1069,22 +1069,11 @@ mrb_ary_splat(mrb_state *mrb, mrb_value v)
}
a = mrb_funcall(mrb, v, "to_a", 0);
- if (mrb_array_p(a)) {
- return a;
- }
- else if (mrb_nil_p(a)) {
+ if (mrb_nil_p(a)) {
return mrb_ary_new_from_values(mrb, 1, &v);
}
- else {
- recv_class = mrb_obj_value(mrb_obj_class(mrb, v));
- mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S to Array (%S#to_a gives %S)",
- recv_class,
- recv_class,
- mrb_obj_value(mrb_obj_class(mrb, a))
- );
- /* not reached */
- return mrb_undef_value();
- }
+ mrb_ensure_array_type(mrb, a);
+ return a;
}
static mrb_value
@@ -1122,12 +1111,6 @@ mrb_ary_empty_p(mrb_state *mrb, mrb_value self)
}
MRB_API mrb_value
-mrb_check_array_type(mrb_state *mrb, mrb_value ary)
-{
- return mrb_check_convert_type(mrb, ary, MRB_TT_ARRAY, "Array", "to_ary");
-}
-
-MRB_API mrb_value
mrb_ary_entry(mrb_value ary, mrb_int offset)
{
if (offset < 0) {
@@ -1180,7 +1163,7 @@ join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list)
val = tmp;
goto str_join;
}
- tmp = mrb_check_convert_type(mrb, val, MRB_TT_ARRAY, "Array", "to_ary");
+ tmp = mrb_check_array_type(mrb, val);
if (!mrb_nil_p(tmp)) {
val = tmp;
goto ary_join;
diff --git a/src/class.c b/src/class.c
index 76c699881..b5a492d8f 100644
--- a/src/class.c
+++ b/src/class.c
@@ -492,34 +492,31 @@ mrb_notimplement_m(mrb_state *mrb, mrb_value self)
return mrb_nil_value();
}
-static mrb_value
-check_type(mrb_state *mrb, mrb_value val, enum mrb_vtype t, const char *c, const char *m)
-{
- mrb_value tmp;
-
- tmp = mrb_check_convert_type(mrb, val, t, c, m);
- if (mrb_nil_p(tmp)) {
- mrb_raisef(mrb, E_TYPE_ERROR, "expected %S", mrb_str_new_cstr(mrb, c));
- }
- return tmp;
-}
+#define CHECK_TYPE(mrb, val, t, c) do { \
+ if (mrb_type(val) != (t)) {\
+ mrb_raisef(mrb, E_TYPE_ERROR, "expected %S", mrb_str_new_lit(mrb, c));\
+ }\
+} while (0)
static mrb_value
to_str(mrb_state *mrb, mrb_value val)
{
- return check_type(mrb, val, MRB_TT_STRING, "String", "to_str");
+ CHECK_TYPE(mrb, val, MRB_TT_STRING, "String");
+ return val;
}
static mrb_value
to_ary(mrb_state *mrb, mrb_value val)
{
- return check_type(mrb, val, MRB_TT_ARRAY, "Array", "to_ary");
+ CHECK_TYPE(mrb, val, MRB_TT_ARRAY, "Array");
+ return val;
}
static mrb_value
to_hash(mrb_state *mrb, mrb_value val)
{
- return check_type(mrb, val, MRB_TT_HASH, "Hash", "to_hash");
+ CHECK_TYPE(mrb, val, MRB_TT_HASH, "Hash");
+ return val;
}
#define to_sym(mrb, ss) mrb_obj_to_sym(mrb, ss)
@@ -1971,7 +1968,7 @@ mrb_mod_const_get(mrb_state *mrb, mrb_value mod)
}
/* const get with class path string */
- path = mrb_string_type(mrb, path);
+ path = mrb_ensure_string_type(mrb, path);
ptr = RSTRING_PTR(path);
len = RSTRING_LEN(path);
off = 0;
diff --git a/src/hash.c b/src/hash.c
index f6b61f4e1..734f6d9bd 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -320,18 +320,6 @@ mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val)
return;
}
-MRB_API mrb_value
-mrb_ensure_hash_type(mrb_state *mrb, mrb_value hash)
-{
- return mrb_convert_type(mrb, hash, MRB_TT_HASH, "Hash", "to_hash");
-}
-
-MRB_API mrb_value
-mrb_check_hash_type(mrb_state *mrb, mrb_value hash)
-{
- return mrb_check_convert_type(mrb, hash, MRB_TT_HASH, "Hash", "to_hash");
-}
-
MRB_API khash_t(ht)*
mrb_hash_tbl(mrb_state *mrb, mrb_value hash)
{
@@ -787,20 +775,6 @@ mrb_hash_empty_m(mrb_state *mrb, mrb_value self)
return mrb_false_value();
}
-/* 15.2.13.4.29 (x)*/
-/*
- * call-seq:
- * hsh.to_hash => hsh
- *
- * Returns +self+.
- */
-
-static mrb_value
-mrb_hash_to_hash(mrb_state *mrb, mrb_value hash)
-{
- return hash;
-}
-
/* 15.2.13.4.19 */
/*
* call-seq:
@@ -1022,6 +996,4 @@ mrb_init_hash(mrb_state *mrb)
mrb_define_method(mrb, h, "store", mrb_hash_aset, MRB_ARGS_REQ(2)); /* 15.2.13.4.26 */
mrb_define_method(mrb, h, "value?", mrb_hash_has_value, MRB_ARGS_REQ(1)); /* 15.2.13.4.27 */
mrb_define_method(mrb, h, "values", mrb_hash_values, MRB_ARGS_NONE()); /* 15.2.13.4.28 */
-
- mrb_define_method(mrb, h, "to_hash", mrb_hash_to_hash, MRB_ARGS_NONE()); /* 15.2.13.4.29 (x)*/
}
diff --git a/src/kernel.c b/src/kernel.c
index db681d510..ce9cd1d44 100644
--- a/src/kernel.c
+++ b/src/kernel.c
@@ -746,6 +746,7 @@ basic_obj_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym id, int pub)
{
return mrb_respond_to(mrb, obj, id);
}
+
/* 15.3.1.3.43 */
/*
* call-seq:
@@ -765,45 +766,16 @@ basic_obj_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym id, int pub)
static mrb_value
obj_respond_to(mrb_state *mrb, mrb_value self)
{
- mrb_value mid;
mrb_sym id, rtm_id;
- mrb_bool priv = FALSE, respond_to_p = TRUE;
-
- mrb_get_args(mrb, "o|b", &mid, &priv);
-
- if (mrb_symbol_p(mid)) {
- id = mrb_symbol(mid);
- }
- else {
- mrb_value tmp;
- if (mrb_string_p(mid)) {
- tmp = mrb_check_intern_str(mrb, mid);
- }
- else {
- tmp = mrb_check_string_type(mrb, mid);
- if (mrb_nil_p(tmp)) {
- tmp = mrb_inspect(mrb, mid);
- mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a symbol", tmp);
- }
- tmp = mrb_check_intern_str(mrb, tmp);
- }
- if (mrb_nil_p(tmp)) {
- respond_to_p = FALSE;
- }
- else {
- id = mrb_symbol(tmp);
- }
- }
-
- if (respond_to_p) {
- respond_to_p = basic_obj_respond_to(mrb, self, id, !priv);
- }
+ mrb_bool priv = FALSE, respond_to_p;
+ mrb_get_args(mrb, "n|b", &id, &priv);
+ respond_to_p = basic_obj_respond_to(mrb, self, id, !priv);
if (!respond_to_p) {
rtm_id = mrb_intern_lit(mrb, "respond_to_missing?");
if (basic_obj_respond_to(mrb, self, rtm_id, !priv)) {
mrb_value args[2], v;
- args[0] = mid;
+ args[0] = mrb_symbol_value(id);
args[1] = mrb_bool_value(priv);
v = mrb_funcall_argv(mrb, self, rtm_id, 2, args);
return mrb_bool_value(mrb_bool(v));
@@ -830,6 +802,7 @@ mrb_obj_ceqq(mrb_state *mrb, mrb_value self)
}
mrb_value mrb_obj_equal_m(mrb_state *mrb, mrb_value);
+
void
mrb_init_kernel(mrb_state *mrb)
{
@@ -871,6 +844,8 @@ mrb_init_kernel(mrb_state *mrb)
mrb_define_method(mrb, krn, "respond_to?", obj_respond_to, MRB_ARGS_ANY()); /* 15.3.1.3.43 */
mrb_define_method(mrb, krn, "to_s", mrb_any_to_s, MRB_ARGS_NONE()); /* 15.3.1.3.46 */
mrb_define_method(mrb, krn, "__case_eqq", mrb_obj_ceqq, MRB_ARGS_REQ(1)); /* internal */
+ mrb_define_method(mrb, krn, "__to_int", mrb_to_int, MRB_ARGS_NONE()); /* internal */
+ mrb_define_method(mrb, krn, "__to_str", mrb_to_str, MRB_ARGS_NONE()); /* internal */
mrb_define_method(mrb, krn, "class_defined?", mrb_krn_class_defined, MRB_ARGS_REQ(1));
diff --git a/src/numeric.c b/src/numeric.c
index f7f0318e8..3624831cc 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -674,7 +674,6 @@ flo_round(mrb_state *mrb, mrb_value num)
/*
* call-seq:
* flt.to_i -> integer
- * flt.to_int -> integer
* flt.truncate -> integer
*
* Returns <i>flt</i> truncated to an <code>Integer</code>.
@@ -714,7 +713,6 @@ flo_nan_p(mrb_state *mrb, mrb_value num)
/*
* call-seq:
* int.to_i -> integer
- * int.to_int -> integer
*
* As <i>int</i> is already an <code>Integer</code>, all these
* methods simply return the receiver.
@@ -1513,7 +1511,6 @@ mrb_init_numeric(mrb_state *mrb)
MRB_SET_INSTANCE_TT(integer, MRB_TT_FIXNUM);
mrb_undef_class_method(mrb, integer, "new");
mrb_define_method(mrb, integer, "to_i", int_to_i, MRB_ARGS_NONE()); /* 15.2.8.3.24 */
- mrb_define_method(mrb, integer, "to_int", int_to_i, MRB_ARGS_NONE());
#ifndef MRB_WITHOUT_FLOAT
mrb_define_method(mrb, integer, "ceil", int_to_i, MRB_ARGS_REQ(1)); /* 15.2.8.3.8 (x) */
mrb_define_method(mrb, integer, "floor", int_to_i, MRB_ARGS_REQ(1)); /* 15.2.8.3.10 (x) */
@@ -1565,7 +1562,6 @@ mrb_init_numeric(mrb_state *mrb)
mrb_define_method(mrb, fl, "round", flo_round, MRB_ARGS_OPT(1)); /* 15.2.9.3.12 */
mrb_define_method(mrb, fl, "to_f", flo_to_f, MRB_ARGS_NONE()); /* 15.2.9.3.13 */
mrb_define_method(mrb, fl, "to_i", flo_truncate, MRB_ARGS_NONE()); /* 15.2.9.3.14 */
- mrb_define_method(mrb, fl, "to_int", flo_truncate, MRB_ARGS_NONE());
mrb_define_method(mrb, fl, "truncate", flo_truncate, MRB_ARGS_NONE()); /* 15.2.9.3.15 */
mrb_define_method(mrb, fl, "divmod", flo_divmod, MRB_ARGS_REQ(1));
mrb_define_method(mrb, fl, "eql?", flo_eql, MRB_ARGS_REQ(1)); /* 15.2.8.3.16 */
diff --git a/src/object.c b/src/object.c
index 8724c5416..66dfa0f97 100644
--- a/src/object.c
+++ b/src/object.c
@@ -323,19 +323,6 @@ convert_type(mrb_state *mrb, mrb_value val, const char *tname, const char *metho
}
MRB_API mrb_value
-mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method)
-{
- mrb_value v;
-
- if (mrb_fixnum_p(val)) return val;
- v = convert_type(mrb, val, "Integer", method, FALSE);
- if (mrb_nil_p(v) || !mrb_fixnum_p(v)) {
- return mrb_nil_value();
- }
- return v;
-}
-
-MRB_API mrb_value
mrb_convert_type(mrb_state *mrb, mrb_value val, enum mrb_vtype type, const char *tname, const char *method)
{
mrb_value v;
@@ -505,25 +492,22 @@ mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c)
return FALSE;
}
-static mrb_value
-mrb_to_integer(mrb_state *mrb, mrb_value val, const char *method)
-{
- mrb_value v;
-
- if (mrb_fixnum_p(val)) return val;
- v = convert_type(mrb, val, "Integer", method, TRUE);
- if (!mrb_obj_is_kind_of(mrb, v, mrb->fixnum_class)) {
- mrb_value type = inspect_type(mrb, val);
- mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S to Integer (%S#%S gives %S)",
- type, type, mrb_str_new_cstr(mrb, method), inspect_type(mrb, v));
- }
- return v;
-}
-
MRB_API mrb_value
mrb_to_int(mrb_state *mrb, mrb_value val)
{
- return mrb_to_integer(mrb, val, "to_int");
+
+ if (!mrb_fixnum_p(val)) {
+ mrb_value type;
+
+#ifndef MRB_WITHOUT_FLOAT
+ if (mrb_float_p(val)) {
+ return mrb_flo_to_fixnum(mrb, val);
+ }
+#endif
+ type = inspect_type(mrb, val);
+ mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S to Integer", type);
+ }
+ return val;
}
MRB_API mrb_value
@@ -533,18 +517,12 @@ mrb_convert_to_integer(mrb_state *mrb, mrb_value val, mrb_int base)
if (mrb_nil_p(val)) {
if (base != 0) goto arg_error;
- mrb_raise(mrb, E_TYPE_ERROR, "can't convert nil into Integer");
+ mrb_raise(mrb, E_TYPE_ERROR, "can't convert nil into Integer");
}
switch (mrb_type(val)) {
#ifndef MRB_WITHOUT_FLOAT
case MRB_TT_FLOAT:
if (base != 0) goto arg_error;
- else {
- mrb_float f = mrb_float(val);
- if (FIXABLE_FLOAT(f)) {
- break;
- }
- }
return mrb_flo_to_fixnum(mrb, val);
#endif
@@ -568,11 +546,8 @@ mrb_convert_to_integer(mrb_state *mrb, mrb_value val, mrb_int base)
arg_error:
mrb_raise(mrb, E_ARGUMENT_ERROR, "base specified for non string value");
}
- tmp = convert_type(mrb, val, "Integer", "to_int", FALSE);
- if (mrb_nil_p(tmp) || !mrb_fixnum_p(tmp)) {
- tmp = mrb_to_integer(mrb, val, "to_i");
- }
- return tmp;
+ /* to raise TypeError */
+ return mrb_to_int(mrb, val);
}
MRB_API mrb_value
@@ -605,6 +580,67 @@ mrb_Float(mrb_state *mrb, mrb_value val)
#endif
MRB_API mrb_value
+mrb_to_str(mrb_state *mrb, mrb_value val)
+{
+ if (!mrb_string_p(val)) {
+ mrb_value type = inspect_type(mrb, val);
+ mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S to String", type);
+ }
+ return val;
+}
+
+MRB_API mrb_value
+mrb_ensure_string_type(mrb_state *mrb, mrb_value str)
+{
+ if (!mrb_string_p(str)) {
+ mrb_raisef(mrb, E_TYPE_ERROR, "%S cannot be converted to String",
+ inspect_type(mrb, str));
+ }
+ return str;
+}
+
+MRB_API mrb_value
+mrb_check_string_type(mrb_state *mrb, mrb_value str)
+{
+ if (!mrb_string_p(str)) return mrb_nil_value();
+ return str;
+}
+
+MRB_API mrb_value
+mrb_ensure_array_type(mrb_state *mrb, mrb_value ary)
+{
+ if (!mrb_array_p(ary)) {
+ mrb_raisef(mrb, E_TYPE_ERROR, "%S cannot be converted to Array",
+ inspect_type(mrb, ary));
+ }
+ return ary;
+}
+
+MRB_API mrb_value
+mrb_check_array_type(mrb_state *mrb, mrb_value ary)
+{
+ if (!mrb_array_p(ary)) return mrb_nil_value();
+ return ary;
+}
+
+MRB_API mrb_value
+mrb_ensure_hash_type(mrb_state *mrb, mrb_value hash)
+{
+ if (!mrb_hash_p(hash)) {
+ mrb_raisef(mrb, E_TYPE_ERROR, "%S cannot be converted to Hash",
+ inspect_type(mrb, hash));
+ }
+ return hash;
+}
+
+MRB_API mrb_value
+mrb_check_hash_type(mrb_state *mrb, mrb_value hash)
+{
+ if (!mrb_hash_p(hash)) return mrb_nil_value();
+ return hash;
+}
+
+MRB_API mrb_value
mrb_inspect(mrb_state *mrb, mrb_value obj)
{
return mrb_obj_as_string(mrb, mrb_funcall(mrb, obj, "inspect", 0));
diff --git a/src/string.c b/src/string.c
index b7abfb762..b6d4ecef0 100644
--- a/src/string.c
+++ b/src/string.c
@@ -956,15 +956,7 @@ str_eql(mrb_state *mrb, const mrb_value str1, const mrb_value str2)
MRB_API mrb_bool
mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2)
{
- if (mrb_immediate_p(str2)) return FALSE;
- if (!mrb_string_p(str2)) {
- if (mrb_nil_p(str2)) return FALSE;
- if (!mrb_respond_to(mrb, str2, mrb_intern_lit(mrb, "to_str"))) {
- return FALSE;
- }
- str2 = mrb_funcall(mrb, str2, "to_str", 0);
- return mrb_equal(mrb, str2, str1);
- }
+ if (!mrb_string_p(str2)) return FALSE;
return str_eql(mrb, str1, str2);
}
@@ -992,14 +984,8 @@ mrb_str_equal_m(mrb_state *mrb, mrb_value str1)
MRB_API mrb_value
mrb_str_to_str(mrb_state *mrb, mrb_value str)
{
- mrb_value s;
-
if (!mrb_string_p(str)) {
- s = mrb_check_convert_type(mrb, str, MRB_TT_STRING, "String", "to_str");
- if (mrb_nil_p(s)) {
- s = mrb_convert_type(mrb, str, MRB_TT_STRING, "String", "to_s");
- }
- return s;
+ return mrb_convert_type(mrb, str, MRB_TT_STRING, "String", "to_s");
}
return str;
}
@@ -1714,18 +1700,6 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
return mrb_obj_value(p_str);
}
-MRB_API mrb_value
-mrb_string_type(mrb_state *mrb, mrb_value str)
-{
- return mrb_convert_type(mrb, str, MRB_TT_STRING, "String", "to_str");
-}
-
-MRB_API mrb_value
-mrb_check_string_type(mrb_state *mrb, mrb_value str)
-{
- return mrb_check_convert_type(mrb, str, MRB_TT_STRING, "String", "to_str");
-}
-
/* 15.2.10.5.30 */
/*
* call-seq:
@@ -2379,7 +2353,6 @@ mrb_str_to_f(mrb_state *mrb, mrb_value self)
/*
* call-seq:
* str.to_s => str
- * str.to_str => str
*
* Returns the receiver.
*/
@@ -2783,7 +2756,6 @@ mrb_init_string(mrb_state *mrb)
#endif
mrb_define_method(mrb, s, "to_i", mrb_str_to_i, MRB_ARGS_ANY()); /* 15.2.10.5.39 */
mrb_define_method(mrb, s, "to_s", mrb_str_to_s, MRB_ARGS_NONE()); /* 15.2.10.5.40 */
- mrb_define_method(mrb, s, "to_str", mrb_str_to_s, MRB_ARGS_NONE());
mrb_define_method(mrb, s, "to_sym", mrb_str_intern, MRB_ARGS_NONE()); /* 15.2.10.5.41 */
mrb_define_method(mrb, s, "upcase", mrb_str_upcase, MRB_ARGS_NONE()); /* 15.2.10.5.42 */
mrb_define_method(mrb, s, "upcase!", mrb_str_upcase_bang, MRB_ARGS_NONE()); /* 15.2.10.5.43 */
diff --git a/test/t/string.rb b/test/t/string.rb
index e91b915fe..3a1eced16 100644
--- a/test/t/string.rb
+++ b/test/t/string.rb
@@ -253,19 +253,6 @@ assert('String#chomp!', '15.2.10.5.10') do
assert_equal 'abc', e
end
-assert('String#chomp! uses the correct length') do
- class A
- def to_str
- $s.replace("AA")
- "A"
- end
- end
-
- $s = "AAA"
- $s.chomp!(A.new)
- assert_equal $s, "A"
-end
-
assert('String#chop', '15.2.10.5.11') do
a = ''.chop
b = 'abc'.chop