-2

What can be the reason of string capitalization not working?

A database column:

t.string   "name",       limit: 255

Some example:

flower_name = Flower.find_by(id: 1).name #=> "chamomile©"

Trying to capitalize (got the same output):

flower_name.capitalize #=> "chamomile©"

Checking if it is string:

flower_name.is_a?(String) #=> true
Nadiya
  • 1,421
  • 4
  • 19
  • 34
  • What do `flower_name.chars` and `flower_name.codepoints` return? – Stefan Mar 01 '16 at 08:13
  • Is it possible that someone overrode the `capitalize` method? – spickermann Mar 01 '16 at 08:23
  • Do a simple test in the console: flower_name = "John Do", the try capitalize again. If it doesn't work, the method might be overwritten. You could remove as many gems as you can an try it again to see if elimination would fix it. – Roger Mar 01 '16 at 08:27

2 Answers2

5

capitalize works with ASCII characters only. Is there any chance your string contains non-ascii letters?

Try

flower_name.mb_chars.capitalize.to_s
dimuch
  • 12,728
  • 2
  • 39
  • 23
2

mb_chars method may help you if you are using Rails >= 3.

 'æ-ý'.mb_chars.upcase

 => "Æ-Ý"

If you're not using Rails, you can:

I hope you will find an answer in this similar question: Special character uppercase

Community
  • 1
  • 1
Yuri Karpovich
  • 382
  • 4
  • 10