Ruby on Rails uncapitalize first letter
There is no inverse of capitalize
, but you can feel free to roll your own:
class String
def uncapitalize
self[0, 1].downcase + self[1..-1]
end
end
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..."
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"
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.
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)*' '
how to use ruby/rails to convert all caps to proper first letter cap, rest lower case?
If you can separate the states out, like you say, it's easy:
my_address_string.titlecase
It'll capitalize the first letter of every word (including some I'd rather it didn't, like "a" or "the", but hey...) and uncapitalize the rest. Sounds just like what you want.
Rails is defaulting to capitalizing only first letter of first word, rather than what I type into string
label_tag
takes one, two or three arguments. If you give it one, it's the name of the label, and it guesses the tag's text. If you want to control the tag's text, give it two arguments:
<%= label_tag thing, thing %>
From the documentation:
label_tag 'name'
# => <label for="name">Name</label>
label_tag 'name', 'Your name'
# => <label for="name">Your name</label>
label_tag 'name', nil, class: 'small_label'
# => <label for="name" class="small_label">Name</label>
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
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.
Related Topics
Does Rails Come with a "Not Authorized" Exception
Rspec Any_Instance Deprecation: How to Fix It
Split Array Up into N-Groups of M Size
Why Is "❨╯°□°❩╯︵┻━┻" with Such an Encoding Used for a Method Name
Rails: Url/Path with Parameters
How to Get Name of the Month in Ruby on Rails
Using Activerecord Interface for Models Backed by External API in Ruby on Rails
How to Tell If I'm Running from Jruby VS. Ruby
Single Occurrence Event with Ice_Cube Gem Using Start_Time and End_Time
Rails 4 Strong Parameters Without Required Parameters
Rails Pass Params/Arguments to Activerecord Callback Function
Initializing Instance Variables in Mixins