views:

170

answers:

4

upcase method capitalizes the entire string.

I need to capitalize only the first letter.

Also, i need support several popular languages, like "german", "russian" etc.

How to do it ?

A: 

Use capitalize. From the docs:

Returns a copy of str with the first character converted to uppercase and the remainder to lowercase.

      "hello".capitalize    #=> "Hello"
      "HELLO".capitalize    #=> "Hello"
      "123ABC".capitalize   #=> "123abc"
jhwist
Only use the exclamation point if you want the original string to be changed.
Magnar
*doh* Thanks, fixed my mistake.
jhwist
-1. The OP *explicitly* mentions German and Russian text, which implies non-ASCII characters. `String#upcase` (and also `String#downcase`) are only defined for ASCII characters.
Jörg W Mittag
+7  A: 

First of all, be sure to use the coding magic comment:

#!/usr/bin/env ruby

puts "мария".capitalize

gives invalid multibyte char (US-ASCII), while:

#!/usr/bin/env ruby
#coding: utf-8

puts "мария".capitalize

works without errors. The problem is, it just doesn't do what you want it to: it outputs мария instead of Мария. If you're using Rails there's an easy workaround:

"мария".mb_chars.capitalize.to_s # requires ActiveSupport::Multibyte

does the job (despite being ugly). Otherwise, you'll have to install the unicode gem and use it like this:

#!/usr/bin/env ruby
#coding: utf-8

require 'unicode'

puts Unicode::capitalize("мария")

This ouputs the right word: Мария.

Alberto
It works ! Thanks a lot !
AntonAL
A: 

@AntonAL you can use the following code if you want it in your rails environment

str = "rohit"
str = str.camelize

and you can use this one in ruby

str = "rohit"
str = str.capitalize

The ruby code executes on both ruby as well as rails environment so no worries.

Rohit
-1. The OP *explicitly* mentions German and Russian text, which implies non-ASCII characters. `String#capitalize` is only defined for ASCII characters.
Jörg W Mittag
my bad I should have analyzed the situation
Rohit
+1  A: 

Unfortunately, it is impossible for a machine to upcase/downcase/capitalize properly. It needs way too much contextual information for a computer to understand.

That's why Ruby's String class only supports capitalization for ASCII characters, because there it's at least somewhat well-defined.

What do I mean by "contextual information"?

For example, to capitalize i properly, you need to know which language the text is in. English, for example, has only two is: capital I without a dot and small i with a dot. But Turkish has four is: capital I without a dot, capital İ with a dot, small ı without a dot, small i with a dot. So, in English 'i'.upcase # => 'I' and in Turkish 'i'.upcase # => 'İ'. In other words: since 'i'.upcase can return two different results, depending on the language, it is obviously impossible to correctly capitalize a word without knowing its language.

But Ruby doesn't know the language, it only knows the encoding. Therefore it is impossible to properly capitalize a string with Ruby's built-in functionality.

It gets worse: even with knowing the language, it is sometimes impossible to do capitalization properly. For example, in German, 'Maße'.upcase # => 'MASSE' (Maße is the plural of Maß meaning measurement). However, 'Masse'.upcase # => 'MASSE' (meaning mass). So, what is 'MASSE'.capitalize? In other words: correctly capitalizing requires a full-blown Artificial Intelligence.

So, instead of sometimes giving the wrong answer, Ruby chooses to sometimes give no answer at all, which is why non-ASCII characters simply get ignored in downcase/upcase/capitalize operations. (Which of course also reads to wrong results, but at least it's easy to check.)

Jörg W Mittag