summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-22 22:39:31 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-22 22:39:31 +0000
commit9b9fe826fd1ce825af3503f39643f75910307a51 (patch)
treef7f17130a3822841cd606414985614fd1c833772
parentc7dafeb20c125f2a647f35e61fa68be9ddb9028d (diff)
{complex,object,rational}.c: document exception: false
From: Victor Shepelev <zverok.offline@gmail.com> [ruby-core:90673] [Bug #15452] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66498 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--complex.c5
-rw-r--r--object.c20
-rw-r--r--rational.c6
3 files changed, 23 insertions, 8 deletions
diff --git a/complex.c b/complex.c
index 35e56283ef..eb2a76737b 100644
--- a/complex.c
+++ b/complex.c
@@ -394,7 +394,7 @@ static VALUE nucomp_s_convert(int argc, VALUE *argv, VALUE klass);
/*
* call-seq:
- * Complex(x[, y]) -> numeric
+ * Complex(x[, y], exception: false) -> numeric
*
* Returns x+i*y;
*
@@ -403,6 +403,9 @@ static VALUE nucomp_s_convert(int argc, VALUE *argv, VALUE klass);
* Complex(nil) #=> TypeError
* Complex(1, nil) #=> TypeError
*
+ * Complex(1, nil, exception: false) # => nil
+ * Complex('1+2', exception: false) # => nil
+ *
* Syntax of string form:
*
* string form = extra spaces , complex , extra spaces ;
diff --git a/object.c b/object.c
index 637a301791..4778118391 100644
--- a/object.c
+++ b/object.c
@@ -3221,7 +3221,7 @@ opts_exception_p(VALUE opts)
/*
* call-seq:
- * Integer(arg, base=0) -> integer
+ * Integer(arg, base=0, exception: true) -> integer
*
* Converts <i>arg</i> to an <code>Integer</code>.
* Numeric types are converted directly (with floating point numbers
@@ -3232,15 +3232,23 @@ opts_exception_p(VALUE opts)
* 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 by first
- * trying <code>to_int</code>, then <code>to_i</code>. Passing <code>nil</code>
- * raises a TypeError.
+ * trying <code>to_int</code>, then <code>to_i</code>.
+ *
+ * Passing <code>nil</code> raises a TypeError, while passing String that
+ * does not conform with numeric representation raises an ArgumentError.
+ * This behavior can be altered by passing <code>exception: false</code>,
+ * in this case not convertible value will return <code>nil</code>.
*
* Integer(123.999) #=> 123
* Integer("0x1a") #=> 26
* Integer(Time.new) #=> 1204973019
* Integer("0930", 10) #=> 930
* Integer("111", 2) #=> 7
- * Integer(nil) #=> TypeError
+ * Integer(nil) #=> TypeError: can't convert nil into Integer
+ * Integer("x") #=> ArgumentError: invalid value for Integer(): "x"
+ *
+ * Integer("x", exception: false) #=> nil
+ *
*/
static VALUE
@@ -3575,17 +3583,19 @@ rb_Float(VALUE val)
/*
* call-seq:
- * Float(arg) -> float
+ * Float(arg, exception: true) -> float
*
* Returns <i>arg</i> converted to a float. Numeric types are converted
* directly, and with exception to string and nil the rest are converted using <i>arg</i>.to_f.
* Converting a <code>string</code> with invalid characters will result in a <code>ArgumentError</code>.
* Converting <code>nil</code> generates a <code>TypeError</code>.
+ * Exceptions could be suppressed by passing <code>exception: false</code>.
*
* Float(1) #=> 1.0
* Float("123.456") #=> 123.456
* Float("123.0_badstring") #=> ArgumentError: invalid value for Float(): "123.0_badstring"
* Float(nil) #=> TypeError: can't convert nil into Float
+ * Float("123.0_badstring", exception: false) #=> nil
*/
static VALUE
diff --git a/rational.c b/rational.c
index a6a94368e8..f668cc4691 100644
--- a/rational.c
+++ b/rational.c
@@ -529,8 +529,8 @@ static VALUE nurat_s_convert(int argc, VALUE *argv, VALUE klass);
/*
* call-seq:
- * Rational(x, y) -> rational
- * Rational(arg) -> rational
+ * Rational(x, y, exception: true) -> rational
+ * Rational(arg, exception: true) -> rational
*
* Returns +x/y+ or +arg+ as a Rational.
*
@@ -546,6 +546,8 @@ static VALUE nurat_s_convert(int argc, VALUE *argv, VALUE klass);
* Rational(nil) #=> TypeError
* Rational(1, nil) #=> TypeError
*
+ * Rational("10 cents", exception: false) #=> nil
+ *
* Syntax of the string form:
*
* string form = extra spaces , rational , extra spaces ;