Rails 5: New upcase_first Method
Have you ever needed to capitalize just the first letter of a
String in Ruby/Rails?
Very likely you tried something like that, no?!
But the result is not the expected one..
String#capitalize converts the first character to uppercase and all others to lowercase.
We can resolve it simply with
sub(/\S/, &:upcase) or any other implementation:
But it is a bit verbose. I wouldn't like to have to call such method whenever I need to capitalize just the first character..
We can add a new method to the
String class as well (and put it into a Rails initializer):
So, as of Rails 5.0.0.beta4 you can use the new
String#upcase_first method or in its