How to Capitalize the First Letter in a String in Ruby

How to capitalize the first letter in a String in Ruby

It depends on which Ruby version you use:

Ruby 2.4 and higher:

It just works, as since Ruby v2.4.0 supports Unicode case mapping:

"мария".capitalize #=> Мария

Ruby 2.3 and lower:

"maria".capitalize #=> "Maria"
"мария".capitalize #=> мария

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

Otherwise, you'll have to install the unicode gem and use it like this:

require 'unicode'

Unicode::capitalize("мария") #=> Мария

Ruby 1.8:

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, but also see the "Ruby 2.3 and lower" section for real capitalization.

Capitalize only first character of string and leave others alone? (Rails)

Titleize will capitalise every word.
This line feels hefty, but will guarantee that the only letter changed is the first one.

new_string = string.slice(0,1).capitalize + string.slice(1..-1)

Update:

irb(main):001:0> string = "i'm from New York..."
=> "i'm from New York..."
irb(main):002:0> new_string = string.slice(0,1).capitalize + string.slice(1..-1)
=> "I'm from New York..."

Ruby capitalize every word first letter

try this:

puts 'one TWO three foUR'.split.map(&:capitalize).join(' ')

#=> One Two Three Four

or

puts 'one TWO three foUR'.split.map(&:capitalize)*' '

Capitalize the first letter of each word - Ruby

Do as below using Array#map :

 text.split.map { |i| i.capitalize }.join(' ')

Corrected and short code :

text= "a bunny hops"
final = text.split.map(&:capitalize).join(' ')
puts final
# >> A Bunny Hops

Why didn't your one worked :

Because Array#each method returns the receiver itself on which it has been called :

text= "a bunny hops"
text.split.each(&:capitalize) # => ["a", "bunny", "hops"]

But Array#map returns a new array

text.split.map(&:capitalize) # => ["A", "Bunny", "Hops"]

I would do it as below using String#gsub:

text= "a bunny hops"
text.gsub(/[A-Za-z']+/,&:capitalize) # => "A Bunny Hops"

Note: The pattern I used here with #gsub, is not the trivial one. I did it as per the string have been given in the post itself. You need to change it as per the text string samples you will be having. But the above is a way to do such things with short code and more Rubyish way.

Capitalize First Letter of all Words and Keep Already Capitalized

You can try the following:

a.split.map{|x| x.slice(0, 1).capitalize + x.slice(1..-1)}.join(' ')
# or
a.split.map{|x| x[0].upcase + x[1..-1]}.join(' ')
#=> ["MDMA Is Also Known As Molly",
"How Far Is McDonald's From Here?",
"I Drive A BMW"]

Demonstration

How to lowercase only the first character of a string in Ruby?

One way:

str = "Hello World"
str[0] = str[0].downcase
str #=> "hello World"


Related Topics



Leave a reply



Submit