I'm pretty much a newbie too, but I'll take a shot. Your pseudo code at the end is pretty much right on.
if url.match(/\:.*\:/) # if it matches two colons anywhere in the string errors.add(:url, "is too long. Easy on the Ctrl-V next time...") end If you want that in one line: errors.add(:url, "is too long...") if url.match(/\:.*\:/) Though I'm sure someone will come up with an even more elegant solution. Hope this helps. Glad to hear you're moving from PHP to Ruby/Rails! Doug On 8/23/06, Patrick Crowley <[EMAIL PROTECTED]> wrote:
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
_______________________________________________ Sdruby mailing list [email protected] http://lists.sdruby.com/mailman/listinfo/sdruby
