Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

`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...
commit 4b36dafb35b99c570be853dc18595b3a43bd72a1 1 parent c55cdd8
Mislav Marohnić mislav authored jeremy committed
4 activesupport/lib/active_support/core_ext/string/starts_ends_with.rb
@@ -20,12 +20,12 @@ def self.append_features(base)
20 20
21 21 # Does the string start with the specified +prefix+?
22 22 def starts_with?(prefix)
23   - self[0, prefix.length] == prefix
  23 + prefix.respond_to?(:to_str) && self[0, prefix.length] == prefix
24 24 end
25 25
26 26 # Does the string end with the specified +suffix+?
27 27 def ends_with?(suffix)
28   - self[-suffix.length, suffix.length] == suffix
  28 + suffix.respond_to?(:to_str) && self[-suffix.length, suffix.length] == suffix
29 29 end
30 30 end
31 31 end
4 activesupport/test/core_ext/string_ext_test.rb
@@ -177,7 +177,7 @@ def test_starts_ends_with_alias
177 177 s = "hello"
178 178 assert s.starts_with?('h')
179 179 assert s.starts_with?('hel')
180   - assert !s.starts_with?(:hel) if :hel.respond_to?(:length)
  180 + assert !s.starts_with?(:hel)
181 181 assert !s.starts_with?('el')
182 182
183 183 assert s.start_with?('h')
@@ -186,7 +186,7 @@ def test_starts_ends_with_alias
186 186
187 187 assert s.ends_with?('o')
188 188 assert s.ends_with?('lo')
189   - assert !s.ends_with?(:lo) if :lo.respond_to?(:length)
  189 + assert !s.ends_with?(:lo)
190 190 assert !s.ends_with?('el')
191 191
192 192 assert s.end_with?('o')

0 comments on commit 4b36daf

Please sign in to comment.
Something went wrong with that request. Please try again.