%w for symbols

Is there any similar notation to %w[ word word2 word3 ] that returns an array of symbols instead of strings?

If not, are there any plans for it in future ruby? Is it a good idea anyway?

(Maybe that's a good example of where we could use macros?)

%w(a b c).map{|i|i.to_sym} #=> [:a, :b, :c]

If that isn't short enough.. then you may want to do

irb(main):001:0> class Array; def to_sym; self.map{|i|i.to_sym}; end; end
=> nil
irb(main):001:0> %w(a b c).to_sym
=> [:a, :b, :c]

IMHO I don't think a literal for easier making array of symbols is needed.

···

On Friday 06 August 2004 09:00, Caio Chassot wrote:

Is there any similar notation to %w[ word word2 word3 ] that returns an
array of symbols instead of strings?

If not, are there any plans for it in future ruby? Is it a good idea
anyway?

(Maybe that's a good example of where we could use macros?)

--
Simon Strandgaard

It seems common enough that it might be nice to have this. Perhaps
%W{} instead of %w{} ?

-austin

···

On Fri, 6 Aug 2004 16:00:32 +0900, Caio Chassot <k@v2studio.com> wrote:

Is there any similar notation to %w[ word word2 word3 ] that returns an
array of symbols instead of strings?

If not, are there any plans for it in future ruby? Is it a good idea anyway?

(Maybe that's a good example of where we could use macros?)

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

%w(a b c).map{|i|i.to_sym} #=> [:a, :b, :c]

If that isn't short enough.. then you may want to do

I guess that pretty much misses the point, otherwise I could just go ahead and write [:a, :b, :c]

thanks anyway

Austin Ziegler wrote:

It seems common enough that it might be nice to have this. Perhaps
%W{} instead of %w{} ?

%W{} is already taken:

irb(main):006:0> %W{1 #{1+1} 3}
=> ["1", "2", "3"]
irb(main):007:0> %w{1 #{1+1} 3}
=> ["1", "#{1+1}", "3"]

And I think that [:foo, :bar, :qux] is already short enough.

Regards,
Florian Gross

I don't object to the feature either. I think %s{} makes more sense,
though.

Gavin

···

On Friday, August 6, 2004, 9:18:12 PM, Austin wrote:

On Fri, 6 Aug 2004 16:00:32 +0900, Caio Chassot <k@v2studio.com> wrote:

Is there any similar notation to %w[ word word2 word3 ] that returns an
array of symbols instead of strings?

If not, are there any plans for it in future ruby? Is it a good idea anyway?

(Maybe that's a good example of where we could use macros?)

It seems common enough that it might be nice to have this. Perhaps
%W{} instead of %w{} ?

are you trying to avoid creating the intermediate strings?

martin

···

Caio Chassot <k@v2studio.com> wrote:

> %w(a b c).map{|i|i.to_sym} #=> [:a, :b, :c]
>
> If that isn't short enough.. then you may want to do

I guess that pretty much misses the point, otherwise I could just go
ahead and write [:a, :b, :c]

And ['foo', 'bar', 'qux'] isn't?

Gavin

···

On Friday, August 6, 2004, 9:21:25 PM, Florian wrote:

And I think that [:foo, :bar, :qux] is already short enough.

Ah, but (to use Gavin's suggested form):

%s(1 2 3) is shorter than [:"1", :"2", :"3"] for any given version.

I don't know how often I'd use this -- I rarely even use %w{} (and
didn't even know about %W{}), except in unit tests. It seems like a
good idea, though.

-austin

···

On Fri, 6 Aug 2004 20:21:25 +0900, Florian Gross <flgr@ccan.de> wrote:

Austin Ziegler wrote:
> It seems common enough that it might be nice to have this. Perhaps
> %W{} instead of %w{} ?
%W{} is already taken:

irb(main):006:0> %W{1 #{1+1} 3}
=> ["1", "2", "3"]
irb(main):007:0> %w{1 #{1+1} 3}
=> ["1", "#{1+1}", "3"]

And I think that [:foo, :bar, :qux] is already short enough.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

%s is already used:

$ ruby
p %s(foo bar baz)
^D
:"foo bar baz"

···

Gavin Sinclair (gsinclair@soyabean.com.au) wrote:

On Friday, August 6, 2004, 9:18:12 PM, Austin wrote:

> On Fri, 6 Aug 2004 16:00:32 +0900, Caio Chassot <k@v2studio.com> wrote:
>> Is there any similar notation to %w[ word word2 word3 ] that returns an
>> array of symbols instead of strings?
>>
>> If not, are there any plans for it in future ruby? Is it a good idea anyway?
>>
>> (Maybe that's a good example of where we could use macros?)

> It seems common enough that it might be nice to have this. Perhaps
> %W{} instead of %w{} ?

I don't object to the feature either. I think %s{} makes more sense,
though.

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Martin DeMello wrote:

%w(a b c).map{|i|i.to_sym} #=> [:a, :b, :c]

If that isn't short enough.. then you may want to do

I guess that pretty much misses the point, otherwise I could just go ahead and write [:a, :b, :c]

are you trying to avoid creating the intermediate strings?

Ideally, yes. But I'm just looking for convinience.

i use them both in almost every program - and would like to see a symbol
version too - but i'm wondering if the OP has noticed that symbols might not
even be needed

   'foobar'.send 'index', 'f' => 0

what i mean is - alot of things that take symbols also take strings. in my
opinion this is as it should be since yaml makes pulling string data into a
program so trivial. since i've started using it everywhere i use symbols less
and less and try to write my own code using the following approach

class Klass

   def method args, opts = {}
     foobar = getopt opts, :foobar
   end

   def getopt hash, opt
     hash[opt] || hash["#{ opt }.intern] || hash[#{ opt }]
   end

end

so the string/symbol equiv will hold. anyhow, just thought i'd point out that
a list of symbols may or may not actually be needed...

i'd vote for the

   %s( foo bar baz )

idea

cheers.

-a

···

On Fri, 6 Aug 2004, Austin Ziegler wrote:

On Fri, 6 Aug 2004 20:21:25 +0900, Florian Gross <flgr@ccan.de> wrote:

Austin Ziegler wrote:

It seems common enough that it might be nice to have this. Perhaps
%W{} instead of %w{} ?

%W{} is already taken:

irb(main):006:0> %W{1 #{1+1} 3}
=> ["1", "2", "3"]
irb(main):007:0> %w{1 #{1+1} 3}
=> ["1", "#{1+1}", "3"]

And I think that [:foo, :bar, :qux] is already short enough.

Ah, but (to use Gavin's suggested form):

%s(1 2 3) is shorter than [:"1", :"2", :"3"] for any given version.

I don't know how often I'd use this -- I rarely even use %w{} (and
didn't even know about %W{}), except in unit tests. It seems like a
good idea, though.

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

Hi --

>
> >> Is there any similar notation to %w[ word word2 word3 ] that returns an
> >> array of symbols instead of strings?
> >>
> >> If not, are there any plans for it in future ruby? Is it a good idea anyway?
> >>
> >> (Maybe that's a good example of where we could use macros?)
>
> > It seems common enough that it might be nice to have this. Perhaps
> > %W{} instead of %w{} ?
>
> I don't object to the feature either. I think %s{} makes more sense,
> though.

%s is already used:

$ ruby
p %s(foo bar baz)
^D
:"foo bar baz"

That actually points to another thing I was wondering about, namely,
what would be the %?{ } equivalent of:

  ["abc".intern, "hi there".intern]

You couldn't do (using %m as a placeholder here):

  %m{abc hi there}

It would have to be

  %m{abc "hi there"}

I'm just wondering whether that would lead down a path of escape
syntax that would make it less streamlined.

David

···

On Fri, 6 Aug 2004, Eric Hodel wrote:

Gavin Sinclair (gsinclair@soyabean.com.au) wrote:
> On Friday, August 6, 2004, 9:18:12 PM, Austin wrote:
> > On Fri, 6 Aug 2004 16:00:32 +0900, Caio Chassot <k@v2studio.com> wrote:

--
David A. Black
dblack@wobblini.net

Eric Hodel wrote:

%s is already used:

%i then? (intern)

symbol words? how about:

%sw( these are symbols )
   => [:these, :are, :symbols]

As far as I can tell, the %?{} literal format allows for strings of indeterminate length. So it's really the %.*{} literal format.

cheers
Mark

···

On Aug 6, 2004, at 7:01 AM, Eric Hodel wrote:

Gavin Sinclair (gsinclair@soyabean.com.au) wrote:

On Friday, August 6, 2004, 9:18:12 PM, Austin wrote:

On Fri, 6 Aug 2004 16:00:32 +0900, Caio Chassot <k@v2studio.com> >>> wrote:

Is there any similar notation to %w[ word word2 word3 ] that returns an
array of symbols instead of strings?

If not, are there any plans for it in future ruby? Is it a good idea anyway?

(Maybe that's a good example of where we could use macros?)

It seems common enough that it might be nice to have this. Perhaps
%W{} instead of %w{} ?

I don't object to the feature either. I think %s{} makes more sense,
though.

%s is already used:

$ ruby
p %s(foo bar baz)
^D
:"foo bar baz"

You couldn't do (using %m as a placeholder here):

  %m{abc hi there}

It would have to be

  %m{abc "hi there"}

I'm just wondering whether that would lead down a path of escape
syntax that would make it less streamlined.

I guess the usual %m{abc hi\ there}

Or is that the escape syntax you're trying to avoid?

we are already down that path

   jib:~ > ruby -r yaml -e 'y %w(abc hi there)'

···

On Fri, 6 Aug 2004, David A. Black wrote:

That actually points to another thing I was wondering about, namely,
what would be the %?{ } equivalent of:

["abc".intern, "hi there".intern]

You couldn't do (using %m as a placeholder here):

%m{abc hi there}

It would have to be

%m{abc "hi there"}

I'm just wondering whether that would lead down a path of escape syntax that
would make it less streamlined.

   ---
   - abc
   - hi
   - there

eg. there is not %w equivalent of ["abc", "hi there"] and this has not been a
problem.

i don't think it would be a problem for symbols either since you could never
really need to do

   obj.send 'hi there'.intern

since you could never define

   def hi there
     42
   end

and you could never need

   hash['hi there'.intern]

since you could not type

   hash[:hi there]

well, never is a strong word - but this issue seems even more unlikely to
occur than needing to do it via a wordlist since symbols containing white
space are far and few between.

regards.

-a
--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

symbol words? how about:

%sw( these are symbols )
  => [:these, :are, :symbols]

As far as I can tell, the %?{} literal format allows for strings of indeterminate length. So it's really the %.*{} literal format.

I like it.

Hi --

> That actually points to another thing I was wondering about, namely,
> what would be the %?{ } equivalent of:
>
> ["abc".intern, "hi there".intern]
>
> You couldn't do (using %m as a placeholder here):
>
> %m{abc hi there}
>
> It would have to be
>
> %m{abc "hi there"}
>
> I'm just wondering whether that would lead down a path of escape syntax that
> would make it less streamlined.

we are already down that path

   jib:~ > ruby -r yaml -e 'y %w(abc hi there)'
   ---
   - abc
   - hi
   - there

eg. there is not %w equivalent of ["abc", "hi there"] and this has not been a
problem.

OK... but that's not exactly the same as the question of the costs and
benefits of introducing a new %? construct.

i don't think it would be a problem for symbols either since you could never
really need to do

   obj.send 'hi there'.intern

since you could never define

   def hi there
     42
   end

and you could never need

   hash['hi there'.intern]

since you could not type

   hash[:hi there]

You could do it like this:

  irb(main):007:0> h = {}
  => {}
  irb(main):008:0> h["hi there".intern] = 1
  => 1
  irb(main):009:0> h[:"hi there"]
  => 1

(i.e., not being able to do [:hi there] doesn't rule this out)

well, never is a strong word - but this issue seems even more unlikely to
occur than needing to do it via a wordlist since symbols containing white
space are far and few between.

Maybe, but people are always talking about using symbols to speed up
hashes, etc.... And symbols *can* act that way, so not accomodating
it in some way would be somewhat arbitrary.

David

···

On Sat, 7 Aug 2004, Ara.T.Howard wrote:

On Fri, 6 Aug 2004, David A. Black wrote:

--
David A. Black
dblack@wobblini.net

Ara.T.Howard wrote:

i don't think it would be a problem for symbols either since you could never
really need to do

  obj.send 'hi there'.intern

since you could never define

  def hi there
    42
  end

Wellll...

irb(main):001:0> class A
irb(main):002:1> define_method "hi there" do puts "Hi, there!"; end
irb(main):003:1> end
=> #<Proc:0x40208870@(irb):2>
irb(main):004:0> A.new.send "hi there"
Hi, there!