Permalink
Browse files

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
  • Loading branch information...
Scott Barron
Scott Barron committed Feb 8, 2006
1 parent 49fad6e commit 2d7309388d2e8236a260a191ffebc02a4add5931
@@ -50,6 +50,7 @@ def first(limit = 1)
# "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 @@ def test_access
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

0 comments on commit 2d73093

Please sign in to comment.