In my last post, I really tried to write about devops since that is what I’m currently doing professionally. But, it just doesn’t tickle my brain. If you, my freakin awesome reader have read the latest StackOverflow Survey, devops is one of the highest paid positions. Judging from that I should be writing more about devops, but I just don’t think it is my passion at the moment. But Ruby is. Or at least for now. So, here’s what I found in Ruby in the past few weeks
If you haven’t seen WAT, I really think you should watch it first before reading on.
Let’s talk about Ruby
Few weeks ago, a co-worker found something really interesting. He posted on our slack channel this line of code. Does anyone know what the output of this is?
(If you were too lazy to open your irb here’s the output:
Mansplaining the WAT
Well, honestly, the real WATs are the code that we just don’t understand. In Gary Bernhardt’s example of the ruby WAT is more like a ruby worst practice. If you’re careful, you won’t really get to see it.
In a pretty long slack thread, one co-worker found out what was going on in that line of code. He just exclaimed, “AH! That is a regex backreference!”.
Cool, interesting, but, I didn’t really understand what he meant.
After a quick research, I did remember that there was a Ruby Tapas for this. I know that I have seen an episode where Avdi briefly touched on ruby backreference using gsub. (If you are not subscribed to his tapas, you should be ashamed. Subscribe now and watch this video).
Let’s do a few experiments. From the tapas, we now know that
\\0 is a back reference. My first
\\0 was that it is the first capture group. But according to the tapas,
\\1 is the
first capture group.
Let’s do another experiment. This time, let’s use one word with more indicators
Well from that, we can infer that
\\1 (1st capture group), is the first letter that matches the
first parenthesis in the regex, or the first match.
\\0 on the other hand is anything that matches
the whole regex.
gsub iterates over the whole string, trying to search for matches, hence having two
 capture groups and two
 capture groups.
If we look at the last output closer, the last
 capture group captured two characters instead
of one. That is because gsub returned the rest of the chars from the capture group.
Back to the first problem
Let’s try the example again but without the back references:
Now using back references:
In Ruby Conference Australia few weeks ago, Katie McLaughlin gave a talk on WAT’s and it has inspired me to dig deep and find out for myself why WAT’s happen. (Her slides are in here.) It is not because the language is bad; it is usually a part of the language that we don’t understand. Understanding is the key.
If you liked this blog post, or if you think I can make this better, I would love to hear from you! Send a tweet or an email; whatever suits your fancy.