Gavin Sinclair
9/4/2003 1:48:00 PM
On Thursday, September 4, 2003, 10:55:27 PM, Thomas wrote:
> Presumably, one can also use a "try..catch" scenario here too?
"try" and "catch" mean nothing in Ruby.
Gavin
> --- Gavin Sinclair <gsinclair@soyabean.com.au> wrote:
>> On Thursday, September 4, 2003, 12:04:47 PM, Jason wrote:
>>
>> > this = "Dec 12"
>> > that, other = /(\w+) (\d+)/.match(this).captures
>> > p that, other
>>
>> > ....but this will run into problems when there''s not a match, and
>> > Regexp#match returns nil. So something like this might be better
>>
>> > if m = /(\w+) (\d+)/.match(this)
>> > that, other = m.captures
>> > # Do stuff with "that" and "other"
>> > else
>> > # We didn''t match, do something about it
>> > end
>>
>>
>> Good point. I''d probably use exception handling.
>>
>> that, other = REGEX.match(STR).captures rescue []
>>
>> The two variables will receive nil if the match fails. That''s
>> slightly dodgy (may catch other exception?) but concise. If you want
>> to handle it more explicitly, then
>>
>> begin
>> that, other = REGEX.match(STR).captures
>> rescue NoMethodError
>> # handle it
>> end
>>
>> Gavin
>>
>>
> =====
> Thomas Adam
> "The Linux Weekend Mechanic" -- www.linuxgazette.com