Return result of block passed to #scan during regex

Posted by Oli on Stack Overflow See other posts from Stack Overflow or by Oli
Published on 2010-04-10T21:42:11Z Indexed on 2010/04/10 21:43 UTC
Read the original article Hit count: 164

Filed under:
|

I've searched and not been able to find the right way of doing what I'm trying to. I read a file line by line and want to create a new object from a regex on each line. I'm not using an XML parser because the data is not well formed and listed below is all I need to get from each line.

I can't seem to get scan to return the result of the block. It just returns the original string. I have temporarily gotten around it by creating this temp variable, but I'm sure there is a better way.

enum = File.foreach(filename)  

enum.map do |line|
   temp = nil
   line.scan(/<cab id="(\w+)" updates="(\d+)"/) { |a,b| temp = Cab.new(a,b) }
   temp
end

Thanks for any help.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about regex