aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2016-03-31 17:53:13 -0300
committerRafael França <rafaelmfranca@gmail.com>2016-03-31 17:53:13 -0300
commit9d36a5d750407822c2825c711faae07bf29252f1 (patch)
tree6805a2f23a2f096e40b2cea9630fb2ef0ba3a210 /activesupport
parent4fd4f31591b27c7e29a9b151bc09d477367eff47 (diff)
parentf2489f493b794ee83a86e746b6240031acb8994e (diff)
downloadrails-9d36a5d750407822c2825c711faae07bf29252f1.tar.gz
rails-9d36a5d750407822c2825c711faae07bf29252f1.tar.bz2
rails-9d36a5d750407822c2825c711faae07bf29252f1.zip
Merge pull request #24377 from bogdanvlviv/fix_upcase_first
Fix method String#upcase_first
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md2
-rw-r--r--activesupport/lib/active_support/core_ext/string/inflections.rb4
-rw-r--r--activesupport/lib/active_support/inflector/methods.rb6
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb8
4 files changed, 16 insertions, 4 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index dd1b347e73..0baa95cdf0 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -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.
diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb
index f301eeac43..7277f51076 100644
--- a/activesupport/lib/active_support/core_ext/string/inflections.rb
+++ b/activesupport/lib/active_support/core_ext/string/inflections.rb
@@ -224,7 +224,9 @@ class String
# 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
diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb
index 3fbc19ddf8..f94e12e14f 100644
--- a/activesupport/lib/active_support/inflector/methods.rb
+++ b/activesupport/lib/active_support/inflector/methods.rb
@@ -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
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index 5c0b60039a..4761ce580c 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -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)