I have the following regex and function to extract emails to an array and while it's working it seems less than optimal to me. Any suggestion to how I might approve this?
@emails = []
matches = @text_document.scan(/\+'(\S+@\S+|\{(?:\w+, *)+\w+\}@[\w.-]+)'/i)
matches.each {|m| m[0].split(',').each {|email| @emails << email } }
Specifically I am seeking something better than nested each'es.
Cheers
EDIT To be completely fair since I liked both answers I gave them both a fair run but since concat is slightly faster and shorter I will mark that as the answer.
require 'benchmark'
CONSTANT = 1
BenchTimes = 1_000_000
EMAILS = "+'one.emaili@domain.com,another.email@domain.se'"
def email
end
def bm_concat
emails = []
EMAILS.scan(/\+'(\S+@\S+|\{(?:\w+, *)+\w+\}@[\w.-]+)'/i) do |matches|
matches.each {|m| emails.concat(m.split(','))}
end
end
def bm_inject
emails = []
EMAILS.scan(/\+'(\S+@\S+|\{(?:\w+, *)+\w+\}@[\w.-]+)'/i) do |matches|
matches.inject([]) {|arr, mails| emails.concat(mails.split(',')) }
end
end
Benchmark.bmbm do |bm|
bm.report("inject:") { BenchTimes.times { bm_inject } }
bm.report("concat:") { BenchTimes.times { bm_concat } }
end
Yields the following output:
Rehearsal -------------------------------------------
inject: 11.030000 0.060000 11.090000 ( 11.145898)
concat: 9.660000 0.050000 9.710000 ( 9.761068)
--------------------------------- total: 20.800000sec
user system total real
inject: 11.620000 0.060000 11.680000 ( 11.795601)
concat: 10.510000 0.050000 10.560000 ( 10.678999)