aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/string
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-04-20 18:31:22 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-04-20 18:31:40 -0700
commit685a53a360d0d8fc4a9f3b49f900621c940a71f2 (patch)
treefa6b38f75a873a21c3e380f1acbab3d019293363 /activesupport/lib/active_support/core_ext/string
parentdb05c73fb6e2294c576ef9889c70940891682c32 (diff)
parent164a94d0bc8c9124ab820506e5ad79496395c026 (diff)
downloadrails-685a53a360d0d8fc4a9f3b49f900621c940a71f2.tar.gz
rails-685a53a360d0d8fc4a9f3b49f900621c940a71f2.tar.bz2
rails-685a53a360d0d8fc4a9f3b49f900621c940a71f2.zip
Merge branch 'master' into cherry
Conflicts: activesupport/CHANGELOG activesupport/lib/active_support/core_ext/class/delegating_attributes.rb activesupport/lib/active_support/core_ext/hash/conversions.rb activesupport/lib/active_support/core_ext/module/attribute_accessors.rb activesupport/lib/active_support/core_ext/string/multibyte.rb activesupport/lib/active_support/core_ext/time/calculations.rb activesupport/lib/active_support/deprecation.rb
Diffstat (limited to 'activesupport/lib/active_support/core_ext/string')
-rw-r--r--activesupport/lib/active_support/core_ext/string/access.rb34
1 files changed, 29 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb
index 7fb21fa4dd..e806b321f1 100644
--- a/activesupport/lib/active_support/core_ext/string/access.rb
+++ b/activesupport/lib/active_support/core_ext/string/access.rb
@@ -41,9 +41,15 @@ module ActiveSupport #:nodoc:
# "hello".first(2) # => "he"
# "hello".first(10) # => "hello"
def first(limit = 1)
- mb_chars[0..(limit - 1)].to_s
+ if limit == 0
+ ''
+ elsif limit >= size
+ self
+ else
+ mb_chars[0...limit].to_s
+ end
end
-
+
# Returns the last character of the string or the last +limit+ characters.
#
# Examples:
@@ -51,7 +57,13 @@ module ActiveSupport #:nodoc:
# "hello".last(2) # => "lo"
# "hello".last(10) # => "hello"
def last(limit = 1)
- (mb_chars[(-limit)..-1] || self).to_s
+ if limit == 0
+ ''
+ elsif limit >= size
+ self
+ else
+ mb_chars[(-limit)..-1].to_s
+ end
end
end
else
@@ -69,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