Feature #4335

String#split for empty string is always empty array

Added by Heesob Park almost 5 years ago. Updated over 3 years ago.



Here is ruby's behavior:

irb(main):001:0> ' '.split('/')
=> [" "]
irb(main):002:0> ''.split('/')
=> []
irb(main):003:0> '/'.split('/')
=> []

Here is python's behavior:
Python 2.4.3 (#1, Jun 11 2009, 14:09:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

' '.split('/')
[' ']
['', '']

Is there any chance of ''.split('/') returns [''] instead of []?

Related issues

Related to Ruby trunk - Feature #3575: String#split is inconsistent with empty string and negative limit Closed 07/15/2010


#1 Updated by Yui NARUSE almost 5 years ago

  • Status changed from Open to Feedback

We don't think we should follow Python's result
and your this proposal doesn't have enough reason to change current behavior even if it breaks compatibility.

I don't say there is no chance but it should be hard that you can show such strong reason.

#2 Updated by Nobuyoshi Nakada almost 5 years ago

'/'.split('/', -1) returns ["", ""].
I don't think it's necessary to be default.

#3 Updated by Hiroshi Nakamura over 3 years ago

  • Description updated (diff)
  • Status changed from Feedback to Rejected

It's intended feature, and -1 is prepared for your purpose as Nobu said. #3575

Also available in: Atom PDF