From 46dd77e0fc0ed75f7679ec9113b2e724d164760e Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 17 May 2015 05:59:36 +0000 Subject: numeric.c: compare_with_zero * numeric.c (compare_with_zero): raise TypeError when not comparable with 0. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50520 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- numeric.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'numeric.c') diff --git a/numeric.c b/numeric.c index 042daafaeb..848e5fa8f5 100644 --- a/numeric.c +++ b/numeric.c @@ -162,6 +162,17 @@ rb_num_to_uint(VALUE val, unsigned int *ret) #define method_basic_p(klass) rb_method_basic_definition_p(klass, mid) +static VALUE +compare_with_zero(VALUE num, ID mid) +{ + VALUE zero = INT2FIX(0); + VALUE r = rb_check_funcall(num, mid, 1, &zero); + if (r == Qundef) { + rb_cmperr(mid, zero); + } + return r; +} + static inline int positive_int_p(VALUE num) { @@ -175,7 +186,7 @@ positive_int_p(VALUE num) if (method_basic_p(rb_cBignum)) return BIGNUM_POSITIVE_P(num); } - return RTEST(rb_funcall(num, mid, 1, INT2FIX(0))); + return RTEST(compare_with_zero(num, mid)); } static inline int @@ -191,7 +202,7 @@ negative_int_p(VALUE num) if (method_basic_p(rb_cBignum)) return BIGNUM_NEGATIVE_P(num); } - return RTEST(rb_funcall(num, mid, 1, INT2FIX(0))); + return RTEST(compare_with_zero(num, mid)); } int -- cgit v1.2.3