From: mike leonard Date: 2008-03-21T21:59:55+09:00 Subject: A question involving variable evaluation Hello friends, I'm working on some code that will iterate through an array of regex patterns and values, returning a value when the corresponding regex is matched: the_string = "foobar" patterns_and_values = [[/(foo)(.*)/, "bar"], [/(bar)/, "foo"]] patterns_and_values.each do |p| the_match = the_string.match(p[0]) if the_match then return p[1] end end To make this more useful, I'd like to be able to use the subexpression globals in the value that is returned, like so: patterns = [[/(foo)(.*)/, $~[0]], [/(bar)/, $~[1]] However, what is actually returned is the value of $~[0] at the time that the patterns array was created, which is nil. Is there a good way to achieve this without resorting to eval? I'd like to keep the syntax of the patterns_and_values array as simple as possible, since this will be exposed to users so that they can add to it as necessary. Thank you kindly in advance (and apologies if the above syntax isn't quite right - I don't have Ruby installed on this machine). --Mike Leonard