Crystal: String.split on empty string

Created on 30 Jun 2017  路  13Comments  路  Source: crystal-lang/crystal

Hello,

I'm surprised to see that this result:

"".split("\n") # => [""]

Shouldn't the result of the split an empty array ? (cf: Ruby's behaviour)

Most helpful comment

I actually think words is better than split. But I'm a bit tired of taking these micro-decisions, doing breaking changes, etc.

In fact, I will have very little time to dedicate to Crystal from now on, so I shouldn't have commented anything. It's better to target other core team members. Sorry...

All 13 comments

But on JavaScript:

console.log(''.split('')) // => []
console.log(''.split('\n')) // => ['']

On Crystal, "".split (without argument) returns [], so I think it is a bug.

Related issue: #1070

No, I don't think it is not a bug. For example, " foo ".split returns ["foo"], in other words String#split without argument ignores leading and trailing spaces, so this behavior is natural. However, the name String#split does not make sense. I think String#words is better for this behavior.

I think no separator versionString#split behavior is corresponding to %w syntax.

The behavior is correct. When you split, you text existing pieces and try to find a delimiter. If no delimiter is found, the original string is returned in the result.

Ruby removes blank results and it's a bit confusing. In #1070 there was a discussion about it.

split without arguments splits on whitespace (any amount of it) and removes empty strings, so "".split will return an empty array. And I don't think we should rename it to words. We just need to document it better, that's it.

@asterite Thank you for answer. I'll close #4646.

@asterite Could you explain why you don't think we should rename String#split to words? (I guess it is breaking-change.) If possible, I abandon #4646 perfectly.

I actually think words is better than split. But I'm a bit tired of taking these micro-decisions, doing breaking changes, etc.

In fact, I will have very little time to dedicate to Crystal from now on, so I shouldn't have commented anything. It's better to target other core team members. Sorry...

In fact, I will have very little time to dedicate to Crystal from now on

:O

After re-reading this, we can probably:

  1. Leave String#split as it is now (String#works isn't good, because a piece might end up being something like "::%" which isn't a word, like, a word in a language)
  2. We can probably add an option to String#split(...) to remove empty results.

The behavior is correct. When you split, you text existing pieces and try to find a delimiter. If no >delimiter is found, the original string is returned in the result.

That is crappy behavior and not Ruby-like. For example "1234".split('') in Ruby returns nice ['1','2','3','4'] why doesn't Crystal??

Because this isn't Ruby and you can just do "1234".chars # => ['1', '2', '3', '4'] to get the same result.

Also "1234".split("") # => ["1", "2", "3", "4"]. I'm not sure what the issue is here?

#chars returns an Array(Char), while the split("") returns an Array(String).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RX14 picture RX14  路  3Comments

oprypin picture oprypin  路  3Comments

pbrusco picture pbrusco  路  3Comments

asterite picture asterite  路  3Comments

TechMagister picture TechMagister  路  3Comments