Forum: Ruby short hand %w

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Corey (Guest)
on 2006-05-10 08:15
how come when i do this m = %w[cat [dog dog] mouse] and then request
m[1] instead of giving me ["dog", "dog"] it gives me "[dog"

do i have to use the shorthand %w for the inner array too?
Corey (Guest)
on 2006-05-10 08:19
Corey wrote:
> how come when i do this m = %w[cat [dog dog] mouse] and then request
> m[1] instead of giving me ["dog", "dog"] it gives me "[dog"
>
> do i have to use the shorthand %w for the inner array too?

i tride it on the inner array and it return %w"[dog" so that doesnt
work, the %w doesnt seem to work accros the board the way i thought it
would.
Suraj N. Kurapati (Guest)
on 2006-05-10 08:29
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Corey wrote:
> Corey wrote:
>> how come when i do this m = %w[cat [dog dog] mouse] and then request
>> m[1] instead of giving me ["dog", "dog"] it gives me "[dog"
>>
>> do i have to use the shorthand %w for the inner array too?
>
> i tride it on the inner array and it return %w"[dog" so that doesnt
> work, the %w doesnt seem to work accros the board the way i thought it
> would.

As mentioned a few weeks ago on this list, you must escape
whitespace within a single "word" using backslashes (a convention of
UNIX shells, AFAIK):

irb(main):003:0> %w(cat dog\ dog mouse)
=> ["cat", "dog dog", "mouse"]

irb(main):004:0> %w(cat dog\ dog mouse this\ is\ \ \ a\ long\ phrase)
=> ["cat", "dog dog", "mouse", "this is   a long phrase"]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEYWuamV9O7RYnKMcRAtEuAJ960nSEOXSqhVVATiCkkMeSOzD80ACeI8NI
cX5/sgIC+1KaCjYe3NTH5Gs=
=bbpe
-----END PGP SIGNATURE-----
Corey (Guest)
on 2006-05-10 08:33
> As mentioned a few weeks ago on this list, you must escape
> whitespace within a single "word" using backslashes (a convention of
> UNIX shells, AFAIK):
>


oh ok thanks i am new to programming and have only been around this
forum for about 3 days now.
Suraj N. Kurapati (Guest)
on 2006-05-10 10:01
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Corey wrote:
> oh ok thanks i am new to programming and have only been around this
> forum for about 3 days now.

Ah, I was trying to give you a hint about ruby-talk archives:
<http://www.ruby-talk.org>. There you can find every discussion that
was ever occurred on the ruby-talk mailing list.

Happy hunting. :-)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEYYFqmV9O7RYnKMcRAu1TAKCDdHYwQ/C+O4ByNklcwH3Y4EE//wCgoV6i
I+j8YuVnk9eIGCzP7LivROc=
=McCI
-----END PGP SIGNATURE-----
This topic is locked and can not be replied to.