subs = {
'center' =>'centre', 'defense' =>'defense',
'behavour' =>'behaviour', 'apologize' =>'apologise',
'maneuver' =>'manoeuvre', 'pediatric' =>'paediatric',
'traveled' =>'travelled', 'honor' =>'honour',
'favorite' =>'favourite', 'esthetics' =>'aesthetics'
}
str = "My Favorite uncle, Max, an honorable gent, is \
often the Center of attention at parties, mainly \
because he has a great sense for Esthetics. \
I apologize for my clumsy maneuver.".squeeze(' ')
str.gsub(/\b\p{Alpha}+\b/) do |word|
key = word.downcase
if subs.key?(key)
new_word = subs[key]
new_word.capitalize! if word.match?(/\A\p{Upper}/)
word = new_word
end
word
end
#=> "My Favourite uncle, Max, an honorable gent, is \
# often the Centre of attention at parties, mainly \
# because he has a great sense for Aesthetics. \
# I apologise for my clumsy manoeuvre."
"honorable"
не изменяется, поскольку он не является ключом в хэше subs
(даже если он содержит ключ "honor"
). Более полный пример может включать это слово в качестве ключа.