Equivalent of perl qw operator


#1

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


#2

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

-Augie


#3

Vlad C. wrote:

Is there a way to write this differently?

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


#4

On 4/11/07, Vlad C. removed_email_address@domain.invalid wrote:

@array = qw( element1 element2 …)

without the quotes.

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

thanks


#5

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”]


#6

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


#7

Awesome! Thanks to all.

vlad