Project

General

Profile

Bug #13105

`String#to_f` and `String#to_r` don't stop at successive underscores

Added by nobu (Nobuyoshi Nakada) over 2 years ago. Updated about 2 years ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-core:78977]

Description

Integer(), Float() and Rational() errs at successive underscores, and String#to_i stops the conversion there.
But String#to_f and String#to_r continue the conversion like as there were just one underscore.
Is this intentional?

"1_2".to_i    #=> 12
"1__2".to_i   #=> 1
"1.__2".to_f  #=> 1.2
"0.1__2".to_r #=> (3/25) == 0.12

Associated revisions

Revision 0b1f6aed
Added by nobu (Nobuyoshi Nakada) about 2 years ago

object.c: make String#to_f consistent with literal

  • object.c (rb_cstr_to_dbl): stop at successive underscores, as
    well as Float literals. [ruby-core:80098] [Bug #13105]

    • _ should be within digits
    • only one _ allowed between digits

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57979
Added by nobu (Nobuyoshi Nakada) about 2 years ago

object.c: make String#to_f consistent with literal

  • object.c (rb_cstr_to_dbl): stop at successive underscores, as
    well as Float literals. [ruby-core:80098] [Bug #13105]

    • _ should be within digits
    • only one _ allowed between digits

Revision 57979
Added by nobu (Nobuyoshi Nakada) about 2 years ago

object.c: make String#to_f consistent with literal

  • object.c (rb_cstr_to_dbl): stop at successive underscores, as
    well as Float literals. [ruby-core:80098] [Bug #13105]

    • _ should be within digits
    • only one _ allowed between digits

Revision 57979
Added by nobu (Nobuyoshi Nakada) about 2 years ago

object.c: make String#to_f consistent with literal

  • object.c (rb_cstr_to_dbl): stop at successive underscores, as
    well as Float literals. [ruby-core:80098] [Bug #13105]

    • _ should be within digits
    • only one _ allowed between digits

Revision a441e89c
Added by nobu (Nobuyoshi Nakada) about 2 years ago

test_rational.rb: invalid exponent

  • test/ruby/test_rational.rb (test_parse): more checks for invalid exponent. [ruby-core:80098] [Bug #13105]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57987 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57987
Added by nobu (Nobuyoshi Nakada) about 2 years ago

test_rational.rb: invalid exponent

  • test/ruby/test_rational.rb (test_parse): more checks for invalid exponent. [ruby-core:80098] [Bug #13105]

Revision 57987
Added by nobu (Nobuyoshi Nakada) about 2 years ago

test_rational.rb: invalid exponent

  • test/ruby/test_rational.rb (test_parse): more checks for invalid exponent. [ruby-core:80098] [Bug #13105]

Revision 57987
Added by nobu (Nobuyoshi Nakada) about 2 years ago

test_rational.rb: invalid exponent

  • test/ruby/test_rational.rb (test_parse): more checks for invalid exponent. [ruby-core:80098] [Bug #13105]

Revision 62fb6147
Added by nobu (Nobuyoshi Nakada) about 2 years ago

rational.c: read_num

  • rational.c (read_num): use rb_int_parse_cstr to parse integer parts, and make String#to_r consistent with #to_i and #to_f. [ruby-core:80098] [Bug #13105]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57989 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57989
Added by nobu (Nobuyoshi Nakada) about 2 years ago

rational.c: read_num

  • rational.c (read_num): use rb_int_parse_cstr to parse integer parts, and make String#to_r consistent with #to_i and #to_f. [ruby-core:80098] [Bug #13105]

Revision 57989
Added by nobu (Nobuyoshi Nakada) about 2 years ago

rational.c: read_num

  • rational.c (read_num): use rb_int_parse_cstr to parse integer parts, and make String#to_r consistent with #to_i and #to_f. [ruby-core:80098] [Bug #13105]

Revision 57989
Added by nobu (Nobuyoshi Nakada) about 2 years ago

rational.c: read_num

  • rational.c (read_num): use rb_int_parse_cstr to parse integer parts, and make String#to_r consistent with #to_i and #to_f. [ruby-core:80098] [Bug #13105]

History

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

  • Subject changed from `String#to_f` and `String#to_r` don't stop successive underscores to `String#to_f` and `String#to_r` don't stop at successive underscores

Updated by matz (Yukihiro Matsumoto) about 2 years ago

  • Assignee changed from matz (Yukihiro Matsumoto) to nobu (Nobuyoshi Nakada)

We should have more consistency here. to_i and to_r should be more restrictive on treating _.
Namely,

  • _ should be within digits
  • only one _ allowed between digits

Matz.

#3

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

  • Status changed from Assigned to Closed

Applied in changeset r57979.


object.c: make String#to_f consistent with literal

  • object.c (rb_cstr_to_dbl): stop at successive underscores, as
    well as Float literals. [ruby-core:80098] [Bug #13105]

    • _ should be within digits
    • only one _ allowed between digits
#4

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

  • Assignee changed from nobu (Nobuyoshi Nakada) to matz (Yukihiro Matsumoto)
  • Status changed from Closed to Open

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

  • Assignee changed from matz (Yukihiro Matsumoto) to nobu (Nobuyoshi Nakada)
#6

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

  • Status changed from Open to Closed

Applied in changeset r57987.


test_rational.rb: invalid exponent

  • test/ruby/test_rational.rb (test_parse): more checks for invalid exponent. [ruby-core:80098] [Bug #13105]
#7

Updated by naruse (Yui NARUSE) about 2 years ago

  • Backport changed from 2.2: REQUIRED, 2.3: REQUIRED, 2.4: REQUIRED to 2.2: WONTFIX, 2.3: WONTFIX, 2.4: WONTFIX

Also available in: Atom PDF