Ran into a weird bug because of this:<p>> x = "<i>a</i>"<p>=> "<i>a</i>"<p>> x.split('*')<p>=> ["", "a"]<p>Expected => ["", "a", ""] or ["a"]<p>It feels bizarre. Anyone has an explanation for this?
Assuming by the notation you mean what jordsmi says and you mean:<p><pre><code> x = "*a*"
</code></pre>
.. then it's not surprising. It works similarly with anything, not just asterisks. For example:<p><pre><code> ",a,".split(",") # => ["", "a"]
</code></pre>
But I don't consider this behavior odd, and it's been the same behavior all the way from 1.8.6 (the oldest implementation I have installed to test with) through 2.2.<p>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.<p>Also remember that Ruby has its heritage in Perl with methods like this, and Perl behaves similarly:<p><pre><code> perl -e "print join(':', split('\*', '*a*'));" # => ":a"</code></pre>
For people not being able to recreate this is because he doesn't mean x = "a" he means x = "yay" with y being asterisks.<p>So<p>x = "yay"<p>x.split("y")<p>=> ["", "a"]