Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string-strtr.rb

Instance Method Summary collapse

Instance Method Details

#strtr(replace_pairs) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/string-strtr.rb', line 2

def strtr(replace_pairs)
  keys = replace_pairs.map {|a, b| a }
  values = replace_pairs.map {|a, b| b }
  catch_regex = /(#{keys.map{|a| Regexp.quote(a) }.join( ')|(' )})/
  
  return self.gsub(catch_regex) do |match|
    values[keys.index(match)]
  end
end