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 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..."
Capitalize each word in an array
The documentation for String#capitalise
says:
Returns a copy of str with the first character converted to uppercase and the remainder to lowercase.
That's not what you're trying to do. So you need to write something custom instead.
For example:
array.map { |string| string.gsub(/\b[a-z]/, &:upcase) }
I'm not clear if/how you plan to handle other input such as all-caps, or hyphenated words, or multiple lines, ... But if your requirements get more detailed then you may need to expand on this implementation.
ruby capitalize doesnt work for the first word of the title
You should use each_with_index instead of each
to get the index
Capitalize every nth character of each word in a string in Ruby
str = 'capitalize every fourth character in this string'
idx = 0
str.gsub(/./) do |c|
case c
when ' '
idx = 0
c
else
idx += 1
(idx % 4).zero? ? c.upcase : c
end
end
#=> "capItalIze eveRy fouRth chaRactEr in thiS strIng"
Related Topics
Why Can't I Use an Integer as a Key Using the New Ruby 1.9.2 Hash Syntax
Undefined Local Variable for Hash in Method Ruby
What Is Your Preferred Way to Produce Charts in a Ruby on Rails Web Application
How to Run Only Specific Tests in Rspec
How to Test If Parameters Exist in Rails
Rspec: "Array.Should == Another_Array" But Without Concern for Order
What's the Difference Between Rspec's Subject and Let? When Should They Be Used or Not
Bundle Install Issue with Libv8 and Rails
Building Ruby with Rbenv and Ruby-Build Fails with Undefined Symbol: Sslv2_Method
Why Is Rails Outputting My Array
Rspec --Init Not Working/ 'Mkd Ir': Invalid Argument - ./C: (Errno::Einval)