Mind blowing Ruby String#split bug?
Ran into a weird bug because of this:
> x = "a"
=> "a"
> x.split('*')
=> ["", "a"]
Expected => ["", "a", ""] or ["a"]
It feels bizarre. Anyone has an explanation for this?
> x = "a"
=> "a"
> x.split('*')
=> ["", "a"]
Expected => ["", "a", ""] or ["a"]
It feels bizarre. Anyone has an explanation for this?
4 comments
[ 3.4 ms ] story [ 17.8 ms ] threadSo
x = "yay"
x.split("y")
=> ["", "a"]
Think about what's necessary when splitting up a row of CSV, say (or even some space limited logs or configs). You want that first 'missing' value to be represented in the output as it affects the position of later values. However, you don't need the final missing value represented as it has no significance - it's merely a trailing null value.
Also remember that Ruby has its heritage in Perl with methods like this, and Perl behaves similarly: