Ruby's split() function makes me feel special (in a bad way)
Quick hand count: who knows what String.split()
does?
Most developers probably do. Python? easy. Javascript? probably. But if you’re a ruby developer, chances are close to nil
. I’m not trying to imply anything about the intelligence or skill of ruby developers, it’s just that the odds are stacked against you.
So, what does String.split()
do?
In the simple case, it takes a separator string. It returns an array of substrings, split on the given string. Like so:
py> "one|two|three".split("|")
["one", "two", "three"]
Simple enough. As an extension, some languages allow you to pass in a num_splits
option. In python, it splits only this many times, like so:
py> "one|two|three".split("|", 1)
["one", "two|three"]
Ruby is similar, although you have to add one to the second argument (it talks about number of returned components, rather than number of splits performed).
Javascript is a bit odd, in that it will ignore the rest of the string if you limit it:
js> "one|two|three".split("|", 2)
["one", "two"]
I don’t like the javascript way, but these are all valid interpretations of split
. So far. And that’s pretty much all you have to know for python and javascript. But ruby? Pull up a seat.