From: Brian Candler Date: 2007-05-02T20:58:21+09:00 Subject: Re: testing and assigning with regexp's On Wed, May 02, 2007 at 08:23:13PM +0900, Tim Hunter wrote: > Mike Steiner wrote: > >I'm new to Ruby, and in a little program I'm writing, I have code like > >this: > > > >str = '' > >if /something/.match(str) > > str = /something/.match(str)[1] > >end > > > >Is there a more elegant way to do this? > > > >Mike Steiner > > > str = '' > m = /something/.match(str) > str = m[1] if m A useful pattern is: str = $1 if /(something)/ =~ str or without the capture: str = $& if /something/ =~ str