mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Clearer String#first and #last edge cases. Fix that 'foo'.first(0) == 'foo' instead of ''
This commit is contained in:
@@ -81,11 +81,23 @@ module ActiveSupport #:nodoc:
|
||||
end
|
||||
|
||||
def first(limit = 1)
|
||||
self[0..(limit - 1)]
|
||||
if limit == 0
|
||||
''
|
||||
elsif limit >= size
|
||||
self
|
||||
else
|
||||
to(limit - 1)
|
||||
end
|
||||
end
|
||||
|
||||
def last(limit = 1)
|
||||
from(-limit) || self
|
||||
if limit == 0
|
||||
''
|
||||
elsif limit >= size
|
||||
self
|
||||
else
|
||||
from(-limit)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user