What is %w

What is the %w used for in ruby?

Hi –

On Tue, 29 Jul 2008, Joel Saltzmanjoelh wrote:

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

What sort of situations is that syntax useful in?

cool, thanks

Hi –

On Tue, 29 Jul 2008, Sam H. wrote:

%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

On Mon, Jul 28, 2008 at 4:33 PM, Sam H. [email protected]
wrote:

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 :slight_smile:

martin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs