diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/access.rb | 1 | ||||
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 6 |
2 files changed, 7 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb index f59690b032..c1503ab3e3 100644 --- a/activesupport/lib/active_support/core_ext/string/access.rb +++ b/activesupport/lib/active_support/core_ext/string/access.rb @@ -50,6 +50,7 @@ module ActiveSupport #:nodoc: # "hello".last(2) # => "lo" # "hello".last(10) # => "hello" def last(limit = 1) + return self if self.length < limit self[(-limit)..-1] end end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 92f57beb5f..e09eb02188 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -77,6 +77,12 @@ class StringInflectionsTest < Test::Unit::TestCase assert_equal "o", s.last assert_equal "llo", s.last(3) + + assert_equal 'x', 'x'.first + assert_equal 'x', 'x'.first(4) + + assert_equal 'x', 'x'.last + assert_equal 'x', 'x'.last(4) end def test_starts_ends_with |