what is %w

D

David A. Black

Hi --

What is the %w used for in ruby?

To create an array of strings:

%w{ one two three } # same as ["one", "two", "three"]

You can use other delimiters, like []. I'm not sure why I've
gravitated toward curly braces for this.


David
 
S

Sam Haskins

What sort of situations is that syntax useful in?

Hi --

What is the %w used for in ruby?

To create an array of strings:

%w{ one two three } # same as ["one", "two", "three"]

You can use other delimiters, like []. I'm not sure why I've
gravitated toward curly braces for this.


David

--
Rails training from David A. Black and Ruby Power and Light:
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!
 
D

David A. Black

Hi --

Hi --

What is the %w used for in ruby?

To create an array of strings:

%w{ one two three } # same as ["one", "two", "three"]

You can use other delimiters, like []. I'm not sure why I've
gravitated toward curly braces for this.
What sort of situations is that syntax useful in?

It's very handy for saving typing. It's just faster. Also, if your
strings happen to have quotation marks in them, you don't have to
escape them. That may not happen every day, and there are other ways
to bring that about, but it could be useful.


David
 
M

Martin DeMello

What sort of situations is that syntax useful in?

When you need an inline array of strings. Compare

MONTHS = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
MONTHS = %w(January February March April May June July August
September October November December)

Try typing them both an and you'll see the use of the syntax :)

martin
 

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
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top