1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix method String#upcase_first

This commit is contained in:
bogdanvlviv 2016-03-31 02:11:19 +03:00
parent a564fecbd0
commit f2489f493b
4 changed files with 16 additions and 4 deletions

View file

@ -1,6 +1,6 @@
* Add `String#upcase_first` method.
*Glauco Custódio*
*Glauco Custódio*, *bogdanvlviv*
* Prevent `Marshal.load` from looping infinitely when trying to autoload a constant
which resolves to a different name.

View file

@ -225,6 +225,8 @@ class String
# Converts just the first character to uppercase.
#
# 'what a Lovely Day'.upcase_first # => "What a Lovely Day"
# 'w'.upcase_first # => "W"
# ''.upcase_first # => ""
def upcase_first
ActiveSupport::Inflector.upcase_first(self)
end

View file

@ -142,9 +142,11 @@ module ActiveSupport
# Converts just the first character to uppercase.
#
# 'what a Lovely Day'.upcase_first # => "What a Lovely Day"
# upcase_first('what a Lovely Day') # => "What a Lovely Day"
# upcase_first('w') # => "W"
# upcase_first('') # => ""
def upcase_first(string)
string[0].upcase.concat(string[1..-1])
string.length > 0 ? string[0].upcase.concat(string[1..-1]) : ''
end
# Capitalizes all the words and replaces some characters in the string to

View file

@ -80,6 +80,14 @@ class StringInflectionsTest < ActiveSupport::TestCase
assert_equal "What a Lovely Day", "what a Lovely Day".upcase_first
end
def test_upcase_first_with_one_char
assert_equal "W", "w".upcase_first
end
def test_upcase_first_with_empty_string
assert_equal "", "".upcase_first
end
def test_camelize
CamelToUnderscore.each do |camel, underscore|
assert_equal(camel, underscore.camelize)