mirror of
https://github.com/github/rails.git
synced 2026-01-28 15:58:03 -05:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user