From: "S.Volkov" Date: 2007-02-09T18:20:08+09:00 Subject: Re: Your favorite bit of ruby code? "Carl Lerche" wrote in message news:ab90312e0702081249ib5fcd61y924299d020f48526@mail.gmail.com... > > I'd like to see some of them! my 2c: ### Fibonacci = Hash.new{ |ht, k| ht[k] = k<2 ? 1 : ht[k-1] + ht[k-2] } # Sample p Fibonacci[100] #-> 573147844013817084101 ### class String # define new String method for 'array' springs replacement def gsubs_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 # Sample puts "AB[^1-2$].XY".gsubs_a( ["AB", "1-2", "XY", "."], ["CC", "99", "ZZ", "+"] ) #-> CC[^99$]+ZZ enjoy! Sergey