Reading Patrick McKenzie’s excellent practical example of metaprogramming, I came across a line of code I didn’t understand:
That line taught me three new things about Ruby:
- The syntax for the subscript operator
[]allows multiple arguments. (It turns out I already knew this in another context:[1,1,2,3,5,7][2,3] => [2,3,5]) - You can subscript a String with a Regexp, returning the first match:
"goal"[/[aeiou]/] => "o"(nilis returned if there is no match). - If you throw in an index
n, then you get thenth capturing group of the first match:"xaabb"[/(.)\1/, 1] => "a"(ornilagain if no match).
That last one is interesting, because it means there’s a concise way I didn’t previously know about to achieve a common regex task: checking if an input string matches a given format, and if so, extracting part of the format. Say we want to pull out the domain from an email address, but complain if we can’t find it:
Before learning this trick I would have either used a temporary match object a la Java, or gritted my teeth and used a global variable Perl-style:
Both of those seem rather verbose. They can be golfed into one-liners, but the readability starts to suffer:
So I’m left wondering what’s the most readable and/or idiomatic style for regexes in Ruby. TMTOWTDI indeed! Even now I know what it means, "xaabb"[/(.)\1/,1] makes me double-take slightly - it’s an unusual way to use [] - but I guess it’s just another Ruby idiosyncracy I’ll come to know and love.
-
mangalcun liked this
-
achetervefa liked this
-
dsampaolo liked this
-
enpause liked this
-
recette-tagada liked this
-
telephonie-mobile liked this
-
misskities liked this
-
exphazox liked this
-
myscndlife reblogged this from samstokes
-
janiceseymour liked this
-
delmar-zehring reblogged this from samstokes
-
branden-cata reblogged this from samstokes
-
antonio-rosal reblogged this from samstokes
-
adopte-un-mec liked this
-
maryrussell liked this
-
mspiky liked this
-
devis-mutuelle-sante1 liked this
-
dhotson liked this
-
sunfox liked this
-
semanticart liked this
-
fistfvck reblogged this from samstokes
-
kylewritescode liked this
-
samstokes posted this

