The code '////'.split('/')
results in []
. While I expected it to be ['', '', '', '', '']
. If this is a feature of ruby, why is it designed like so?
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.
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.
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.
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.
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.
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?
“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.
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.
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
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"]
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('␣')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With