Hi --

On Sun, 23 Apr 2006, Pawel Szymczykowski wrote:

> On 4/23/06, clc <clc1024 / hotmail.com> wrote:
>> I've been using these two lines to capture text that only appears once, but
>> the second line bothers me a little.  Is there a one-line idiom for this
>> that extracts groups like #scan but returns a string instead of an array so
>> the second line isn't needed?
>>
>> text = string.scan( %r{ skip_this ( keep_this ) skip_this }is )
>> text = text[ 0 ]
>
> Any reason why
>
> text = string.scan( %r{ skip_this ( keep_this ) skip_this }is ).first
>
> or
>
> text = string.scan( %r{ skip_this ( keep_this ) skip_this }is )[0]
>
> wouldn't work?

They'll do the same thing as Chris's example, though they all return
arrays rather than strings:

>> "abc".scan(/a(b)c/)[0]
=> ["b"]

so a further extraction would be necessary.


David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!