From 2e9357e7ea4c35c0ea101487bc2e417622b36b62 Mon Sep 17 00:00:00 2001 From: mrkn Date: Sat, 12 Nov 2016 06:07:01 +0000 Subject: [PATCH] rational.c: optimize Rational#-@ * rational.c (nurat_negate): introduced to optimize Rational#-@. Author: Tadashi Saito git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56735 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- rational.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rational.c b/rational.c index afd6e5033a..1d569ce55a 100644 --- a/rational.c +++ b/rational.c @@ -637,6 +637,19 @@ nurat_denominator(VALUE self) return dat->den; } +/* + * call-seq: + * -rat -> rational + * + * Negates +rat+. + */ +static VALUE +nurat_negate(VALUE self) +{ + get_dat1(self); + return f_rational_new2(CLASS_OF(self), rb_int_uminus(dat->num), dat->den); +} + #ifndef NDEBUG #define f_imul f_imul_orig #endif @@ -2591,6 +2604,7 @@ Init_Rational(void) rb_define_method(rb_cRational, "numerator", nurat_numerator, 0); rb_define_method(rb_cRational, "denominator", nurat_denominator, 0); + rb_define_method(rb_cRational, "-@", nurat_negate, 0); rb_define_method(rb_cRational, "+", rb_rational_plus, 1); rb_define_method(rb_cRational, "-", nurat_sub, 1); rb_define_method(rb_cRational, "*", nurat_mul, 1);