From 164a94d0bc8c9124ab820506e5ad79496395c026 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 20 Apr 2009 00:40:15 -0700 Subject: Clearer String#first and #last edge cases. Fix that 'foo'.first(0) == 'foo' instead of '' --- .../lib/active_support/core_ext/string/access.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/string/access.rb') 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 -- cgit v1.2.3