diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-04-20 00:40:15 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-04-20 00:40:15 -0700 |
commit | 164a94d0bc8c9124ab820506e5ad79496395c026 (patch) | |
tree | d07005b9dc75e6caab53d6555e2dcd169cee43ce | |
parent | 5d84c732ee06f58732167b74ae51d94ca216df12 (diff) | |
download | rails-164a94d0bc8c9124ab820506e5ad79496395c026.tar.gz rails-164a94d0bc8c9124ab820506e5ad79496395c026.tar.bz2 rails-164a94d0bc8c9124ab820506e5ad79496395c026.zip |
Clearer String#first and #last edge cases. Fix that 'foo'.first(0) == 'foo' instead of ''
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/access.rb | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb index e067f930da..e806b321f1 100644 --- a/activesupport/lib/active_support/core_ext/string/access.rb +++ b/activesupport/lib/active_support/core_ext/string/access.rb @@ -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 |