Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does '////'.split('/') produce []?

Tags:

ruby

The code '////'.split('/') results in []. While I expected it to be ['', '', '', '', '']. If this is a feature of ruby, why is it designed like so?

like image 631
Shou Ya Avatar asked Dec 14 '13 11:12

Shou Ya


People also ask

Why does split return a list?

In the case of splitting an empty string, the first mode (no argument) will return an empty list because the whitespace is eaten and there are no values to put in the result list.

What is string split?

Definition and Usage. The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Does Split always return a list?

split() Return ValueThe return value of the split() method is always a list of strings obtained after breaking the given string by the specified separator.

What happens if you split an empty string Java?

Using split() When the string is empty and no separator is specified, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.

What is water splitting?

Water splitting is the chemical reaction in which water is broken down into oxygen and hydrogen : 2 H 2 O → 2 H 2 + O 2 Efficient and economical water splitting would be a technological breakthrough that could underpin a hydrogen economy, based on green hydrogen.

Why can't atoms be split?

And atoms really are pretty atomic under most circumstances: splitting an atom is very difficult, and you can do a lot of chemistry by rounding that up to "impossible". If a single atom has so much energy stored, why don't we make atomic batteries?

How does splitting an atom release energy?

“Splitting an atom “ releases energy when the original nucleus of the atom has more mass than the toatal mass of smaller nucleii into which the original atom splits.

Is it possible to split water and produce hydrogen?

A version of water splitting occurs in photosynthesis, but hydrogen is not produced. No practical version of water splitting has been demonstrated, but the two component reactions (H2 production and O2 production) are well known. The reverse of water splitting is the basis of the hydrogen fuel cell.


3 Answers

You can't split string of delimiters by delimiter.

You should pass limit as second parameter to split function to achieve this behaviour

'////'.split('/',-1)
=> 
["", "", "", "", ""]

If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed

like image 157
SergeyKutsko Avatar answered Oct 26 '22 13:10

SergeyKutsko


Investigation of behaviour of split method show that it is result of optimization, it simply crops empty array elements after last match as it is shewn below:

'////'.split('/')
=> []

'//a//'.split('/')
=> ["", "", "a"]
like image 30
Малъ Скрылевъ Avatar answered Oct 26 '22 13:10

Малъ Скрылевъ


This design provides a convenience for parsing strings with trailing delimiters. For example:

'1␣2␣3␣␣'.split('␣') will now give ['1', '2', '3'] rather than ['1', '2', '3', '', ''].

This feature is just for simplification of workflow.

However, I don't like this feature because it breaks the purity of this method. To achieve the effect above, you just need an extra rstrip('␣') between '1␣2␣3␣␣' and split('␣').

like image 3
Shou Ya Avatar answered Oct 26 '22 13:10

Shou Ya