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:
parent
a564fecbd0
commit
f2489f493b
4 changed files with 16 additions and 4 deletions
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue