Skip to content

Commit 14e7543

Browse files
[DOC] Tweaks for String#to_r
1 parent 0ca3eed commit 14e7543

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

doc/string.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@
402402
# - #to_c: Returns the complex value of leading characters, interpreted as a complex number.
403403
# - #to_i: Returns the integer value of leading characters, interpreted as an integer.
404404
# - #to_f: Returns the floating-point value of leading characters, interpreted as a floating-point number.
405+
# - #to_r: Returns the rational value of leading characters, interpreted as a rational.
405406
#
406407
# <em>Strings and Symbols</em>
407408
#

rational.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,31 +2467,32 @@ string_to_r_strict(VALUE self, int raise)
24672467

24682468
/*
24692469
* call-seq:
2470-
* str.to_r -> rational
2471-
*
2472-
* Returns the result of interpreting leading characters in +str+
2473-
* as a rational. Leading whitespace and extraneous characters
2474-
* past the end of a valid number are ignored.
2475-
* Digit sequences can be separated by an underscore.
2476-
* If there is not a valid number at the start of +str+,
2477-
* zero is returned. This method never raises an exception.
2478-
*
2479-
* ' 2 '.to_r #=> (2/1)
2480-
* '300/2'.to_r #=> (150/1)
2481-
* '-9.2'.to_r #=> (-46/5)
2482-
* '-9.2e2'.to_r #=> (-920/1)
2483-
* '1_234_567'.to_r #=> (1234567/1)
2484-
* '21 June 09'.to_r #=> (21/1)
2485-
* '21/06/09'.to_r #=> (7/2)
2486-
* 'BWV 1079'.to_r #=> (0/1)
2487-
*
2488-
* NOTE: "0.3".to_r isn't the same as 0.3.to_r. The former is
2489-
* equivalent to "3/10".to_r, but the latter isn't so.
2470+
* str.to_r -> rational
24902471
*
2491-
* "0.3".to_r == 3/10r #=> true
2492-
* 0.3.to_r == 3/10r #=> false
2472+
* Returns the result of interpreting leading characters in +self+ as a rational value:
2473+
*
2474+
* '123'.to_r # => (123/1) # Integer literal.
2475+
* '300/2'.to_r # => (150/1) # Rational literal.
2476+
* '-9.2'.to_r # => (-46/5) # Float literal.
2477+
* '-9.2e2'.to_r # => (-920/1) # Float literal.
2478+
*
2479+
* Ignores leading and trailing whitespace, and trailing non-numeric characters:
2480+
*
2481+
* ' 2 '.to_r # => (2/1)
2482+
* '21-Jun-09'.to_r # => (21/1)
2483+
*
2484+
* Returns \Rational zero if there are no leading numeric characters.
2485+
*
2486+
* 'BWV 1079'.to_r # => (0/1)
2487+
*
2488+
* NOTE: <tt>'0.3'.to_r</tt> is equivalent to <tt>3/10r</tt>,
2489+
* but is different from <tt>0.3.to_r</tt>:
2490+
*
2491+
* '0.3'.to_r # => (3/10)
2492+
* 3/10r # => (3/10)
2493+
* 0.3.to_r # => (5404319552844595/18014398509481984)
24932494
*
2494-
* See also Kernel#Rational.
2495+
* Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString].
24952496
*/
24962497
static VALUE
24972498
string_to_r(VALUE self)

0 commit comments

Comments
 (0)