Make String#last behave more like Array#last, i.e.

"f".last(3) => "f"  not
"f".last(3) => nil


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3548 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Scott Barron
2006-02-08 00:49:41 +00:00
parent 49fad6e6d5
commit 2d7309388d
2 changed files with 7 additions and 0 deletions

View File

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

View File

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