Sergey Volkov
4/12/2006 8:42:00 AM
I'm not Ruby expert, but this may work for you:
---------------
$ cat tGsub_a.rb
class String
# define new String method for 'array' substitutions
def gsub_a originals, replacements
# create replacements table
orig2repl = Hash[ *originals.zip( replacements ).flatten ]
# regexp matching any original
regexp = /#{originals.map{ |s| Regexp.escape s }.join( '|' )}/
# substitute each original with replacement from table
self.gsub( regexp ){ |orig| orig2repl[ orig ] }
end
end
puts "AB[^1-2$].XY".gsub_a(
["AB", "1-2", "XY", "."],
["CC", "99", "ZZ", "+"]
)
$ ruby tGsub_a.rb
CC[^99$]+ZZ
---------------
enjoy Rubying!
Sergey Volkov