[前][次][番号順一覧][スレッド一覧]

ruby-changes:68526

From: Burdette <ko1@a...>
Date: Tue, 19 Oct 2021 08:35:28 +0900 (JST)
Subject: [ruby-changes:68526] 012cafa5c7 (master): Enhanced RDoc for numerics (#4982)

https://git.ruby-lang.org/ruby.git/commit/?id=012cafa5c7

From 012cafa5c7274ef50e6306cf5c3e09c2fb64f44d Mon Sep 17 00:00:00 2001
From: Burdette Lamar <BurdetteLamar@Y...>
Date: Mon, 18 Oct 2021 18:35:06 -0500
Subject: Enhanced RDoc for numerics (#4982)

Treats:

    Numeric#coerce
    Numeric#clone
    Numeric#dup
    Numeric#@+ (unary plus)
    Numeric#i
    Float#coerce
---
 numeric.c | 90 ++++++++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 63 insertions(+), 27 deletions(-)

diff --git a/numeric.c b/numeric.c
index 7f6a5f036c..db2b2eb279 100644
--- a/numeric.c
+++ b/numeric.c
@@ -387,19 +387,37 @@ num_funcall1(VALUE x, ID func, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L387
 
 /*
  *  call-seq:
- *     num.coerce(numeric)  ->  array
+ *    coerce(other) -> array
  *
- *  If +numeric+ is the same type as +num+, returns an array
- *  <code>[numeric, num]</code>. Otherwise, returns an array with both
- *  +numeric+ and +num+ represented as Float objects.
+ *  Returns a 2-element array containing two numeric elements,
+ *  formed from the two operands +self+ and +other+,
+ *  of a common compatible type.
  *
- *  This coercion mechanism is used by Ruby to handle mixed-type numeric
- *  operations: it is intended to find a compatible common type between the two
- *  operands of the operator.
+ *  Of the Core and Standard Library classes,
+ *  Integer, Rational, and Complex use this implementation.
+ *
+ *  Examples:
+ *
+ *    i = 2                    # => 2
+ *    i.coerce(3)              # => [3, 2]
+ *    i.coerce(3.0)            # => [3.0, 2.0]
+ *    i.coerce(Rational(1, 2)) # => [0.5, 2.0]
+ *    i.coerce(Complex(3, 4))  # Raises RangeError.
+ *
+ *    r = Rational(5, 2)       # => (5/2)
+ *    r.coerce(2)              # => [(2/1), (5/2)]
+ *    r.coerce(2.0)            # => [2.0, 2.5]
+ *    r.coerce(Rational(2, 3)) # => [(2/3), (5/2)]
+ *    r.coerce(Complex(3, 4))  # => [(3+4i), ((5/2)+0i)]
+ *
+ *    c = Complex(2, 3)        # => (2+3i)
+ *    c.coerce(2)              # => [(2+0i), (2+3i)]
+ *    c.coerce(2.0)            # => [(2.0+0i), (2+3i)]
+ *    c.coerce(Rational(1, 2)) # => [((1/2)+0i), (2+3i)]
+ *    c.coerce(Complex(3, 4))  # => [(3+4i), (2+3i)]
+ *
+ *  Raises an exception if any type conversion fails.
  *
- *     1.coerce(2.5)   #=> [2.5, 1.0]
- *     1.2.coerce(3)   #=> [3.0, 1.2]
- *     1.coerce(2)     #=> [2, 1]
  */
 
 static VALUE
@@ -509,9 +527,14 @@ num_sadded(VALUE x, VALUE name) https://github.com/ruby/ruby/blob/trunk/numeric.c#L527
 #if 0
 /*
  *  call-seq:
- *     num.clone(freeze: true)  ->  num
+ *    clone(freeze: true) -> self
+ *
+ *  Returns +self+.
+ *
+ *  Raises an exception if the value for +freeze+ is neither +true+ nor +nil+.
+ *
+ *  Related: Numeric#dup.
  *
- *  Returns the receiver.  +freeze+ cannot be +false+.
  */
 static VALUE
 num_clone(int argc, VALUE *argv, VALUE x)
@@ -525,9 +548,12 @@ num_clone(int argc, VALUE *argv, VALUE x) https://github.com/ruby/ruby/blob/trunk/numeric.c#L548
 #if 0
 /*
  *  call-seq:
- *     num.dup  ->  num
+ *    dup -> self
+ *
+ *  Returns +self+.
+ *
+ *  Related: Numeric#clone.
  *
- *  Returns the receiver.
  */
 static VALUE
 num_dup(VALUE x)
@@ -540,9 +566,10 @@ num_dup(VALUE x) https://github.com/ruby/ruby/blob/trunk/numeric.c#L566
 
 /*
  *  call-seq:
- *     +num  ->  num
+ *    +num -> self
+ *
+ *  Returns +self+.
  *
- *  Unary Plus---Returns the receiver.
  */
 
 static VALUE
@@ -553,13 +580,16 @@ num_uplus(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L580
 
 /*
  *  call-seq:
- *     num.i  ->  Complex(0, num)
+ *    i -> complex
+ *
+ *  Returns <tt>Complex(0, self)</tt>:
  *
- *  Returns the corresponding imaginary number.
- *  Not available for complex numbers.
+ *    2.i              # => (0+2i)
+ *    -2.i             # => (0-2i)
+ *    2.0.i            # => (0+2.0i)
+ *    Rational(1, 2).i # => (0+(1/2)*i)
+ *    Complex(3, 4).i # Raises NoMethodError.
  *
- *     -42.i  #=> (0-42i)
- *     2.0.i  #=> (0+2.0i)
  */
 
 static VALUE
@@ -985,15 +1015,21 @@ flo_to_s(VALUE flt) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1015
 
 /*
  *  call-seq:
- *     float.coerce(numeric)  ->  array
+ *    coerce(other) -> array
+ *
+ *  Returns a 2-element array containing +other+ converted to a \Float
+ *  and +self+:
+ *
+ *    f = 3.14                 # => 3.14
+ *    f.coerce(2)              # => [2.0, 3.14]
+ *    f.coerce(2.0)            # => [2.0, 3.14]
+ *    f.coerce(Rational(1, 2)) # => [0.5, 3.14]
+ *    f.coerce(Complex(3, 4))  # Raises RangeError.
  *
- *  Returns an array with both +numeric+ and +float+ represented as Float
- *  objects.
+ *  Related: Numeric#coerce (for other numeric types).
  *
- *  This is achieved by converting +numeric+ to a Float.
+ *  Raises an exception if a type conversion fails.
  *
- *     1.2.coerce(3)       #=> [3.0, 1.2]
- *     2.5.coerce(1.1)     #=> [1.1, 2.5]
  */
 
 static VALUE
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]