Generation of array with symbols

Hi:

I was wondering, to create an array I usually do

%w{a b c} #=> [“a”, “b”, “c”]

Is there a built-in way to do this and get symbols?
For example:

%w{:a :b :c} #=> [:a, :b, :c]

or given a mixed list

%w{:a b :c} #=> [:a, “b”, :c]

···


Jim Freeze

Have you noticed the way people’s intelligence capabilities decline
sharply the minute they start waving guns around?
– Dr. Who

Hi:

Hi,

I was wondering, to create an array I usually do

%w{a b c} #=> [“a”, “b”, “c”]

Is there a built-in way to do this and get symbols?
For example:

%w{:a :b :c} #=> [:a, :b, :c]

or given a mixed list

%w{:a b :c} #=> [:a, “b”, :c]

Not that I know of, but of course there is

$ ruby -e ‘p %w{a b c}.map {|s| s.intern}’
[:a, :b, :c]

which is not much longer…

Regards,

Robert

···

On Mon, 20 Jan 2003, Jim Freeze wrote:

Hi:

I was wondering, to create an array I usually do

%w{a b c} #=>> [“a”, “b”, “c”]

Is there a built-in way to do this and get symbols?
For example:

%w{:a :b :c} #=>> [:a, :b, :c]

or given a mixed list

%w{:a b :c} #=>> [:a, “b”, :c]

If there were a built-in way to do this, I’m sure we’d both know about
it. It’s easy to write a method to do it, of course. But you knew
that.

class Array
def to_sym
map { |e|
s = e.to_s
if s[0] == ?:
s[1…-1].intern
else
s
end
}
end
end

%w{:a b :c}.to_sym # => [:a, “b”, :c]

Gavin

···

On Tuesday, January 21, 2003, 12:38:24 AM, Jim wrote:

Hi –

···

On Mon, 20 Jan 2003, Gavin Sinclair wrote:

On Tuesday, January 21, 2003, 12:38:24 AM, Jim wrote:

Hi:

I was wondering, to create an array I usually do

%w{a b c} #=>> [“a”, “b”, “c”]

Is there a built-in way to do this and get symbols?
For example:

%w{:a :b :c} #=>> [:a, :b, :c]

Those are strings, not symbols.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav