Permalink
Browse files

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

… 'foo' instead of ''
  • Loading branch information...
1 parent 5d84c73 commit 164a94d0bc8c9124ab820506e5ad79496395c026 @jeremy jeremy committed Apr 20, 2009
Showing with 14 additions and 2 deletions.
  1. +14 −2 activesupport/lib/active_support/core_ext/string/access.rb
@@ -81,11 +81,23 @@ def to(position)
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

0 comments on commit 164a94d

Please sign in to comment.