Permalink
Browse files

`String#starts/ends_with?` should return false for non-string argumen…

…t, not raise error

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent c55cdd8 commit 4b36dafb35b99c570be853dc18595b3a43bd72a1 @mislav mislav committed with jeremy Apr 17, 2010
View
4 activesupport/lib/active_support/core_ext/string/starts_ends_with.rb
@@ -20,12 +20,12 @@ def self.append_features(base)
# Does the string start with the specified +prefix+?
def starts_with?(prefix)
- self[0, prefix.length] == prefix
+ prefix.respond_to?(:to_str) && self[0, prefix.length] == prefix
end
# Does the string end with the specified +suffix+?
def ends_with?(suffix)
- self[-suffix.length, suffix.length] == suffix
+ suffix.respond_to?(:to_str) && self[-suffix.length, suffix.length] == suffix
end
end
end
View
4 activesupport/test/core_ext/string_ext_test.rb
@@ -177,7 +177,7 @@ def test_starts_ends_with_alias
s = "hello"
assert s.starts_with?('h')
assert s.starts_with?('hel')
- assert !s.starts_with?(:hel) if :hel.respond_to?(:length)
+ assert !s.starts_with?(:hel)
assert !s.starts_with?('el')
assert s.start_with?('h')
@@ -186,7 +186,7 @@ def test_starts_ends_with_alias
assert s.ends_with?('o')
assert s.ends_with?('lo')
- assert !s.ends_with?(:lo) if :lo.respond_to?(:length)
+ assert !s.ends_with?(:lo)
assert !s.ends_with?('el')
assert s.end_with?('o')

0 comments on commit 4b36daf

Please sign in to comment.