Clearer String#first and #last edge cases. Fix that 'foo'.first(0) == 'foo' instead of ''

This commit is contained in:
Jeremy Kemper
2009-04-20 00:40:15 -07:00
parent 63d0c33787
commit e70272e2a4

View File

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