I need to test for URL submissions that contain duplicate protocols -- which usually means a user pasted a link into a text field too many times.

In PHP, I'd use the substr_match function for this... but I can't find a good Ruby replacement.

Here's the test code I've written so far:

# fake url
url = 'http:/site.com/link.ics http://site.com/link.ics http:// site.com/link.ics'

#count the number of times '://' appears
count = 0
url.scan /:\/\// do |match|
  count += 1
end

#is this a bad url?
if count > 2
  puts 'this is a bad url'
end

This works, but doesn't seem like the Ruby way. Is there a better path?

....

Ultimately, I'm aiming for some crazy one-line Ruby magic like this...

if url.some.ruby.method./(:\/\/)/ > 2
  errors.add(:url, "is too long. Bad cut and paste?")
end

Best,
Patrick


_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby

Reply via email to