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

@ -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.

@ -224,7 +224,9 @@ def humanize(options = {})
# Converts just the first character to uppercase.
#
# 'what a Lovely Day'.upcase_first # => "What a Lovely Day"
# '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 @@ def humanize(lower_case_and_underscored_word, options = {})
# 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 @@ def test_upcase_first
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)