From c6f439a6a8df582416e756d7511aa4d9c72071a9 Mon Sep 17 00:00:00 2001 From: "S.H" Date: Fri, 28 Oct 2022 01:13:16 +0900 Subject: Improve performance some `Integer` and `Float` methods [Feature #19085] (#6638) * Improve some Integer and Float methods * Using alias and Remove unnecessary code * Remove commentout code --- numeric.rb | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 9 deletions(-) (limited to 'numeric.rb') diff --git a/numeric.rb b/numeric.rb index c2091465f8..f026679210 100644 --- a/numeric.rb +++ b/numeric.rb @@ -6,7 +6,17 @@ class Numeric # Returns +true+ if +num+ is a real number (i.e. not Complex). # def real? - return true + true + end + + # + # call-seq: + # num.real -> self + # + # Returns self. + # + def real + self end # @@ -19,7 +29,7 @@ class Numeric # 1.integer? #=> true # def integer? - return false + false end # @@ -29,7 +39,7 @@ class Numeric # Returns +true+ if +num+ is a finite number, otherwise returns +false+. # def finite? - return true + true end # @@ -40,8 +50,34 @@ class Numeric # finite, -Infinity, or +Infinity. # def infinite? - return nil + nil + end + + # + # call-seq: + # num.imag -> 0 + # num.imaginary -> 0 + # + # Returns zero. + # + def imaginary + 0 + end + + alias imag imaginary + + # + # call-seq: + # num.conj -> self + # num.conjugate -> self + # + # Returns self. + # + def conjugate + self end + + alias conj conjugate end class Integer @@ -146,7 +182,7 @@ class Integer # # Since +int+ is already an Integer, this always returns +true+. def integer? - return true + true end alias magnitude abs @@ -178,7 +214,7 @@ class Integer # # For example, ?a.ord returns 97 both in 1.8 and 1.9. def ord - return self + self end # @@ -208,7 +244,7 @@ class Integer # # #to_int is an alias for #to_i. def to_i - return self + self end # call-seq: @@ -216,7 +252,7 @@ class Integer # # Since +int+ is already an Integer, returns +self+. def to_int - return self + self end # call-seq: @@ -244,6 +280,26 @@ class Integer def ceildiv(other) -div(-other) end + + # + # call-seq: + # int.numerator -> self + # + # Returns self. + # + def numerator + self + end + + # + # call-seq: + # int.denominator -> 1 + # + # Returns 1. + # + def denominator + 1 + end end # call-seq: @@ -276,7 +332,7 @@ class Float # Since +float+ is already a Float, returns +self+. # def to_f - return self + self end # -- cgit v1.2.3