Hi --

On Tue, 2 Sep 2003, Michael Campbell wrote:

> (I think...)
>
> Having a brain-fade today.  Is there a simple ruby equivalent to
> perl's qw() construct?  I want, in order to save typing, to convert a
> list of non-quoted words like so:
>
> foo bar baz...
>
> into an array of quoted strings: ["foo", "bar", "baz", ...]

%w{one two three}   # => ["one","two","three"]

> Is there a way to get matched data into variables without using
> matchdata, or perl ugly-variables?

You mean you want Ruby to give you *three* systems for doing this? :-)

> Again, in perl I could do something like:
>
> $_ = "Dec 12";
> (mon, day) = /(\w+) (\d+)/;

As of 1.8.0, one way would be:

  require 'scanf'
  mon, day = "Dec 12".scanf("%s%d")

(or "%s%s" if you want 12 as a string rather than an integer)

Or (also 1.8.0):

  mon,day = /(\w+) (\d+)/.match("Dec 12").captures

Or you could do:

  mon,day = *"Dec 12".scan(/(\w+) (\d+)/)

(if you really have some reason to want to avoid dealing explicitly with
a MatchData object)


David

-- 
David Alan Black
home: dblack / superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav