Equivalent of perl qw operator

I have a feeling I’ve seen it in the pickaxe book but I can’t seem to
find
it again.

Is there a way to write this differently?

array = [ ‘element1’, ‘element2’, …]

In perl I could do

@array = qw( element1 element2 …)

without the quotes.

thanks
vlad

Page 15 of the Pick Axe book, as a matter of fact. It’s %w{ }

-Augie

Vlad C. wrote:

Is there a way to write this differently?

array = [ ‘element1’, ‘element2’, …
%w{element1 element2}

On 4/11/07, Vlad C. [email protected] wrote:

@array = qw( element1 element2 …)

without the quotes.

irb(main):001:0> @array = %w{element1 element2}
=> [“element1”, “element2”]

thanks

On Thu, Apr 12, 2007 at 07:19:28AM +0900, Augie De Blieck Jr. wrote:

Page 15 of the Pick Axe book, as a matter of fact. It’s %w{ }

Also . . . if you want to get string interpolation as though you used
double quotes around your list elements, rather than single quotes, you
can capitalize the W in that. In other words:

irb(main):001:0> foo = ‘one’
=> “one”
irb(main):002:0> bar = %w{ #{foo} two }
=> ["#{foo}", “two”]
irb(main):003:0> bar = %W{ #{foo} two }
=> [“one”, “two”]

Vlad C. wrote:

array = [ ‘element1’, ‘element2’, …]

In perl I could do

@array = qw( element1 element2 …)

without the quotes.

thanks
vlad

Also note that you can use other delimiters besides { and }:
irb(main):002:0> %w| bob jack harry |
=> [“bob”, “jack”, “harry”]
irb(main):003:0> %w( bob jack harry )
=> [“bob”, “jack”, “harry”]
irb(main):005:0> %w[ bob jack harry ]
=> [“bob”, “jack”, “harry”]
irb(main):006:0> %w< bob jack harry >
=> [“bob”, “jack”, “harry”]
irb(main):007:0> %w@ bob jack harry @
=> [“bob”, “jack”, “harry”]
irb(main):010:0> %w! bob jack harry !
=> [“bob”, “jack”, “harry”]

The following don’t work:

irb(main):004:0> %w) bob jack harry (
irb(main):005:0]

^C
irb(main):008:0> %www bob jack harry www
SyntaxError: compile error
(irb):8: unknown type of %string
%www bob jack harry www
^
from (irb):8

Awesome! Thanks to all.

vlad