equivalent of perl qw operator

V

Vlad Ciubotariu

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
 
C

Chad Perrin

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"]
 
R

Reuben Grinberg

Vlad said:
Awesome! Thanks to all.

vlad
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top