aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2006-10-13 20:27:46 +0000
committerJamis Buck <jamis@37signals.com>2006-10-13 20:27:46 +0000
commitd73f32ce0004ddb1a8470ed50c9f8323ebc5613f (patch)
treeb332416f8e88bf583a2734b05a88f2536be21d24
parent2c6b6e2866c89b8158811c649c5292eda5d24012 (diff)
downloadrails-d73f32ce0004ddb1a8470ed50c9f8323ebc5613f.tar.gz
rails-d73f32ce0004ddb1a8470ed50c9f8323ebc5613f.tar.bz2
rails-d73f32ce0004ddb1a8470ed50c9f8323ebc5613f.zip
make sure the String::Access methods return strings, and not multibyte Char instances
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5299 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activesupport/lib/active_support/core_ext/string/access.rb10
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb27
2 files changed, 32 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 cb6da4ab6a..d70d889f07 100644
--- a/activesupport/lib/active_support/core_ext/string/access.rb
+++ b/activesupport/lib/active_support/core_ext/string/access.rb
@@ -10,7 +10,7 @@ module ActiveSupport #:nodoc:
# "hello".at(4) # => "o"
# "hello".at(10) # => nil
def at(position)
- chars[position, 1]
+ chars[position, 1].to_s
end
# Returns the remaining of the string from the +position+ treating the string as an array (where 0 is the first character).
@@ -20,7 +20,7 @@ module ActiveSupport #:nodoc:
# "hello".from(2) # => "llo"
# "hello".from(10) # => nil
def from(position)
- chars[position..-1]
+ chars[position..-1].to_s
end
# Returns the beginning of the string up to the +position+ treating the string as an array (where 0 is the first character).
@@ -30,7 +30,7 @@ module ActiveSupport #:nodoc:
# "hello".to(2) # => "hel"
# "hello".to(10) # => "hello"
def to(position)
- chars[0..position]
+ chars[0..position].to_s
end
# Returns the first character of the string or the first +limit+ characters.
@@ -40,7 +40,7 @@ module ActiveSupport #:nodoc:
# "hello".first(2) # => "he"
# "hello".first(10) # => "hello"
def first(limit = 1)
- chars[0..(limit - 1)]
+ chars[0..(limit - 1)].to_s
end
# Returns the last character of the string or the last +limit+ characters.
@@ -50,7 +50,7 @@ module ActiveSupport #:nodoc:
# "hello".last(2) # => "lo"
# "hello".last(10) # => "hello"
def last(limit = 1)
- chars[(-limit)..-1] || self
+ (chars[(-limit)..-1] || self).to_s
end
end
end
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index 47f772ab9b..7eff505a22 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -84,6 +84,33 @@ class StringInflectionsTest < Test::Unit::TestCase
assert_equal 'x', 'x'.last(4)
end
+ def test_access_returns_a_real_string
+ hash = {}
+ hash["h"] = true
+ hash["hello123".at(0)] = true
+ assert_equal %w(h), hash.keys
+
+ hash = {}
+ hash["llo"] = true
+ hash["hello".from(2)] = true
+ assert_equal %w(llo), hash.keys
+
+ hash = {}
+ hash["hel"] = true
+ hash["hello".to(2)] = true
+ assert_equal %w(hel), hash.keys
+
+ hash = {}
+ hash["hello"] = true
+ hash["123hello".last(5)] = true
+ assert_equal %w(hello), hash.keys
+
+ hash = {}
+ hash["hello"] = true
+ hash["hello123".first(5)] = true
+ assert_equal %w(hello), hash.keys
+ end
+
def test_starts_ends_with
s = "hello"
assert s.starts_with?('h')