I just ran in to a really interesting result from Ruby, and one that my google-fu turns up no other references to. When you split a string, you can use a regex as your split delimiter. It’s pretty handy. I’m trying to do some parsing on an Emacs org-mode file, so I wanted to split the text of the file on any number of *’s at the beginning of the line, followed by a space. Simple regex, right? Yes, but with a catch. I will want to know how many stars there were matched, and split strips out the delimiters.Ok, I thought, I’ll put my delimiters in a group and use that. If I’d stopped and though about split for a minute, I would have known that wouldn’t work. What would it capture? The group matched at the last split? I thought it would work since I was immediately calling each on the split string. Ok, so unsurprisingly, $1 is nil inside my each. But what is surprising: the group was then included in the array generated by split. Not so helpful for the each, but an interesting approach. My googling has turnedup nothing about this. It’s a very interesting mechanism. Here’s some sample code:

string = "joe says hello there bob to jane who says hello there bob to sue"
string.split(/ hello there bob /).each do |i|
  puts i
end

string.split(/ hello (there) bob /).each do |i|
  puts i
end

The first split and each outputs:

joe says
to jane who says
to sue

The second split and each outputs:

joe says
there
to jane who says
there
to sue

Not quite what I was trying to achieve, but certainly an interesting result.