%w| a b c| %w_a b c_ %w<a b c> all work

  • Thread starter SpringFlowers AutumnMoon
  • Start date
S

SpringFlowers AutumnMoon

%w|a b c|

%w_a b c_

%w<a b c>

all work
is there an official doc or book that says so?

seems like the first character that after "%w" can be used as a
delimiter.
 
D

David A. Black

Hi --

%w|a b c|

%w_a b c_

%w<a b c>

all work
is there an official doc or book that says so?

seems like the first character that after "%w" can be used as a
delimiter.

Pretty much, though some characters won't work. My favorite % trick
is:

ruby -e 'p %q abc '
"abc"

:)


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!
 
7

7stud --

SpringFlowers said:
all work
is there an official doc or book that says so?

If you look up %w at the beginning of the index in pickaxe2, it directs
you to p. 322, which has a section titled Arrays. In that section, it
says %w is a form of general delimited input described on p. 318-319.
Looking on those pages, it says:

"Following the type character is a delimiter, which can be any
nonalphabetic or nonmultibyte character. If the delimiter is one of the
characters (, [, {, or <, ....blah, blah, blah...[the ending character
must be: ), ], }, or > respectively]. For all other delimiters, [the
ending character is the next occurrence of the starting delimiter]."
 
7

7stud --

7stud said:
"Following the type character is a delimiter, which can be any
nonalphabetic or nonmultibyte character. If the delimiter is one of the
characters (, [, {, or <, ....blah, blah, blah...[the ending character
must be: ), ], }, or > respectively]. For all other delimiters, [the
ending character is the next occurrence of the starting delimiter]."

I just wanted to point out that the statement that the delimiter can be
any "nonalphabetic or nonmultibyte" character appears to be false. The
character '2' meets the condition of being "nonalphabetic or
nonmultibyte", yet it produces an error:

arr = %w_a b c_
p arr #["a", "b", "c"]


arr = %w2a b c2
p arr

r4test.rb:4: unknown type of %string
arr = %w1a b c1
^
r4test.rb:4: syntax error
arr = %w1a b c1
^

It looks like the docs should say:
 
7

7stud --

7stud said:
r4test.rb:4: unknown type of %string
arr = %w1a b c1
^
r4test.rb:4: syntax error
arr = %w1a b c1
^

Whoops. I pasted the wrong error message. It should be:

r4test.rb:4: unknown type of %string
arr = %w2a b c2
^
r4test.rb:4: syntax error
arr = %w2a b c2
^
 
W

William James

David said:
Hi --



Pretty much, though some characters won't work. My favorite % trick
is:

ruby -e 'p %q abc '
"abc"

:)

I enjoy doing this:

irb(main):002:0> %w) foo bar)
=> ["foo", "bar"]
 
J

Joel VanderWerf

William said:
I enjoy doing this:

irb(main):002:0> %w) foo bar)
=> ["foo", "bar"]

You, sir, are demented %w)

(And, no, I don't know what that means as a smiley.)
 

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,264
Messages
2,571,323
Members
48,008
Latest member
KieraMcGuf

Latest Threads

Top