[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: String#start_with? / #end_with?

Mark Wilson

9/15/2003 2:08:00 PM


On Monday, September 15, 2003, at 05:26 AM, Robert Klemme wrote:

>
> "Mark Wilson" <mwilson13@cox.net> 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