From: Eleo Date: 2007-05-26T16:44:38+09:00 Subject: gsub + loop This question actually pertains to a Rails app but it's more of a general Ruby question so I'll ask it here. Within a body of text I'm trying to match URLs for youtube/google/myspace/etc videos and replace them with their associated embed codes. For each different site there is a different regular expression and a different embed code. So I made a hash with each value being an array containing the regular expression and replacement like: {:videosite => [regexp, replacement]}. From there I figured I should loop through the hash and check the text against each expression and replace the URLs if necessary. Unfortunately I am having a problem: http://pastie.caboo.se/64785 class Embedment < ActiveRecord::Base def self.capture_embedments(text) embedments = [] regexps.each do |k,v| text.gsub!(v[0]) do |match| embedments << embedment = self.new embedment.html = v[1] end end return embedments end def self.regexps return { :youtube => [/\(youtube:.*?(?:v=)?([\w|-]{11}).*\)/, ""] } end end I think the flaw in my plan is that the second member of the array is being parsed as soon as it's referenced, so this raises an exception (undefined local variable or method `match' for Embedment:Class) since the object 'match' does not exist yet. I'm guessing my approach to this problem is very very wrong, but I have yet to see past my poor solution. The reason why I separated the regular expressions from the method is because there's going to be a few tens of them and I wanted to consolidate them. Any help would be appreciated. -- Posted via http://www.ruby-forum.com/.