I’m looking for a much more idiomatic way to do the following little ruby script.
File.open("channels.xml").each do |line|
if line.match('(mms:\/\/{1}[a-zA-Z\.\d\/\w-]+)')
puts line.match('(mms:\/\/{1}[a-zA-Z\.\d\/\w-]+)')
end
end
Thanks in advance for any suggestions.
The original:
can be changed into this:
File.opencan be changed to justopen.can be changed to
puts x if x = XYZas long as x has occurred at some place in the current scope before the if statement.The Regexp
'(mms:\/\/{1}[a-zA-Z\.\d\/\w-]+)'can be refactored a little bit. Using the%rXXnotation, you can create regular expressions without the need for so many backslashes, where X is any matching character, such as(and)or in the example above,||.This character class
[a-zA-Z\.\d\/\w-](read: A to Z, case insensitive, the period character, 0 to 9, a forward slash, any word character, or a dash) is a little redundant.\wdenotes “word characters”, i.e. A-Za-z0-9 and underscore. Since you specify\was a positive match,A-Za-zand\dare redundant.Using those 2 cleanups, the Regexp can be changed into this:
%r|(mms://{1}[\w\./-]+)|If you’d like to avoid the weird
m = nilscoping sorcery, this will also work, but is less idiomatic:or the longer, but more readable version: