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(/[æøåäâãàáëêèéïîìíöôõòóüûùúý]/, {
        'æ' => 'Æ',
        'ø' => 'Ø',
        'å' => 'Å',
        'ä' => 'Ä',
        'â' => 'Â',
        'ã' => 'Ã',
        'à' => 'À',
        'á' => 'Á',
        'ë' => 'Ë',
        'ê' => 'Ê',
        'è' => 'È',
        'é' => 'É',
        'ï' => 'Ï',
        'î' => 'Î',
        'ì' => 'Ì',
        'í' => 'Í',
        'ö' => 'Ö',
        'ô' => 'Ô',
        'õ' => 'Õ',
        'ò' => 'Ò',
        'ó' => 'Ó',
        'ü' => 'Ü',
        'û' => 'Û',
        'ù' => 'Ù',
        'ú' => 'Ú',
        'ý' => 'Ý'

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?


2 Answers 2


One-to-one substitution is precisely what tr is for.

def norwegian_upcase(string)

That being said, the unicode_utils gem provides a method for this:

def norwegian_upcase(string)
  UnicodeUtils.upcase(string, :no)

I don't know Norwegian, but you can supply language subtags :nb (Norwegian Bokmal) or :nn (Norwegian Nynorsk) if they would behave differently than general :no (Norwegian).

  • \$\begingroup\$ I tried UnicodeUtils without success, it simply doesn't upcase æøå to ÆØÅ no matter what language i pass to it. I tried :en, :no, :nb and :nn. I haven't tried tr, but I'll check that out. \$\endgroup\$
    – Hubro
    Jun 6, 2013 at 13:58

After a cursory glance at the character codes for these, it looks like the lowercase is 32 (decimal) higher than uppercase. e.g. 'é'.ord - 32 == 'É'.ord

You could try something like this:

string.upcase.gsub(/[æøåäâãàáëêèéïîìíöôõòóüûùúý]/){|s| (s.ord-32).chr(Encoding::UTF_8)}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.