From: Mark Wilson Date: 2003-09-15T23:07:50+09:00 Subject: Re: String#start_with? / #end_with? On Monday, September 15, 2003, at 05:26 AM, Robert Klemme wrote: > > "Mark Wilson" schrieb im Newsbeitrag > news:319AF784-E56A-11D7-BBD2-000393876156@cox.net... >> >> On Friday, September 12, 2003, at 05:20 PM, Philip Mak wrote: >> >>> [snip] >> >> class String >> >> def start_with?(little_string) >> !self.match(/^#{Regexp.escape(little_string)}/).nil? >> end > > Would you care to explain why you test for nil? Why not: > > def start_with?(little_string) > match(/^#{Regexp.escape(little_string)}/) > end > > or even > > def start_with?(little_string) > little_string.empty? || match(/^#{Regexp.escape(little_string)}/) > end > [snip] Totally mundane reasons -- I was extending Ryan Pavlik's initial suggestion in light of Philip Mak's further question. I chose to test for nil? so that the method returned true or false. Incidentally, I like Gavin Sinclair's alternate version on the RubyGardenWiki for character matching as opposed to Regexp matching plus escaping Regexp special characters. Regards, Mark