aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-04-20 00:40:15 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-04-20 00:40:15 -0700
commit164a94d0bc8c9124ab820506e5ad79496395c026 (patch)
treed07005b9dc75e6caab53d6555e2dcd169cee43ce /activesupport/lib/active_support/core_ext
parent5d84c732ee06f58732167b74ae51d94ca216df12 (diff)
downloadrails-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 ''
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/string/access.rb16
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