[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

what is %w

Joel Saltzmanjoelh

7/28/2008 6:35:00 PM

What is the %w used for in ruby?
--
Posted via http://www.ruby-....

4 Answers

David A. Black

7/28/2008 6:39:00 PM

0

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

--
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.r... for details and updates!

Sam Haskins

7/28/2008 11:33:00 PM

0

What sort of situations is that syntax useful in?

On Jul 28, 2008, at 2:39 PM, David A. Black wrote:

> 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
>
> --
> 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.r... for details and updates!
>


David A. Black

7/28/2008 11:44:00 PM

0

Hi --

On Tue, 29 Jul 2008, Sam Haskins wrote:

> On Jul 28, 2008, at 2:39 PM, David A. Black wrote:
>
>> 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.
>>
> 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

--
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.r... for details and updates!

Martin DeMello

7/28/2008 11:48:00 PM

0

On Mon, Jul 28, 2008 at 4:33 PM, Sam Haskins <sam.haskins@gmail.com> 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 :)

martin