Skip to main content
Code Review

Return to Question

Should I be using Regex for my upper-case functionto uppercase uncommon characters?

Source Link
Hubro
  • 161
  • 8

Should I be using Regex for my upper-case function?

I've written a function for converting strings to upper case. It currently works by replacing each character using a Regex pattern with a hash:

# Special upcase function that handles several Norwegian symbols.
def norwegian_upcase(string)
 string.upcase.gsub(/[æøåäâãàáëêèéïîìíöôõòóüûùúý]/, {
 'æ' => 'Æ',
 'ø' => 'Ø',
 'å' => 'Å',
 'ä' => 'Ä',
 'â' => 'Â',
 'ã' => 'Ã',
 'à' => 'À',
 'á' => 'Á',
 'ë' => 'Ë',
 'ê' => 'Ê',
 'è' => 'È',
 'é' => 'É',
 'ï' => 'Ï',
 'î' => 'Î',
 'ì' => 'Ì',
 'í' => 'Í',
 'ö' => 'Ö',
 'ô' => 'Ô',
 'õ' => 'Õ',
 'ò' => 'Ò',
 'ó' => 'Ó',
 'ü' => 'Ü',
 'û' => 'Û',
 'ù' => 'Ù',
 'ú' => 'Ú',
 'ý' => 'Ý'
 })
end

I have the feeling that it's horribly inefficient to be using Regex like this, and that I should probably be using another method. Can anyone suggest a better way to replace single characters, or is Regex fit for the task?

lang-rb

AltStyle によって変換されたページ (->オリジナル) /