3 ms·
The inevitable ruby style niggling, feel free to ignore: personally I like something a little more concise and inlined, some parts seem to have used more verbos
by davesims 14y ago
The inevitable ruby style niggling, feel free to ignore: personally I like something a little more concise and inlined, some parts seem to have used more verbose idioms, (like Regexp.new instead of a literal), and also I think you need to escape the backreferences or use single quotes, something like:
(words - @@nouns).each do |w|
text.gsub!(/(\b)#{w}(\b)/i, '\1<span class="nurble">nurble</span>\2')
end
EDIT: oops, backslash. Also, in the example, line 19, where you have 'sub', don't you mean 'pattern'?
- adambard 14y agoI submitted this article expecting to learn a few things about my style. In this case, I didn't actually know you could interpolate into regexes like that, so thanks! And thanks for the typo catch, too.
- davesims 14y agoNo problem, in ruby style is always pretty subjective since there's a thousand ways to do just about everything. And, just FYI you do need to escape the backrefs or use single quotes, like: "\\1<span class=\"nurble\">nurble</span>\\2"
- davesims 14y agoI take it back - I had issues with escaping on irb but not in the app itself. Something new to learn...
- deleted 14y ago[deleted]