Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

%w for symbols

0 views
Skip to first unread message

Caio Chassot

unread,
Aug 6, 2004, 3:00:32 AM8/6/04
to
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

unread,
Aug 6, 2004, 3:07:00 AM8/6/04
to

%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.

--
Simon Strandgaard


Caio Chassot

unread,
Aug 6, 2004, 3:13:20 AM8/6/04
to
> %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

Martin DeMello

unread,
Aug 6, 2004, 3:38:15 AM8/6/04
to

are you trying to avoid creating the intermediate strings?

martin

Caio Chassot

unread,
Aug 6, 2004, 4:20:46 AM8/6/04
to
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.


Austin Ziegler

unread,
Aug 6, 2004, 7:18:12 AM8/6/04
to

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

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


Florian Gross

unread,
Aug 6, 2004, 7:20:00 AM8/6/04
to
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

Gavin Sinclair

unread,
Aug 6, 2004, 8:37:24 AM8/6/04
to
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.

Gavin


Gavin Sinclair

unread,
Aug 6, 2004, 8:38:55 AM8/6/04
to
On Friday, August 6, 2004, 9:21:25 PM, Florian wrote:

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

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

Gavin


Austin Ziegler

unread,
Aug 6, 2004, 8:48:00 AM8/6/04
to

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.

Eric Hodel

unread,
Aug 6, 2004, 10:01:20 AM8/6/04
to
Gavin Sinclair (gsin...@soyabean.com.au) wrote:

%s is already used:

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

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

Ara.T.Howard

unread,
Aug 6, 2004, 10:12:25 AM8/6/04
to
On Fri, 6 Aug 2004, Austin Ziegler wrote:

> On Fri, 6 Aug 2004 20:21:25 +0900, Florian Gross <fl...@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.

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
--
===============================================================================
| 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
===============================================================================

David A. Black

unread,
Aug 6, 2004, 10:41:27 AM8/6/04
to
Hi --

On Fri, 6 Aug 2004, Eric Hodel wrote:

> Gavin Sinclair (gsin...@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"

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

--
David A. Black
dbl...@wobblini.net

Caio Chassot

unread,
Aug 6, 2004, 11:18:04 AM8/6/04
to
Eric Hodel wrote:
>
> %s is already used:
>

%i then? (intern)

Caio Chassot

unread,
Aug 6, 2004, 11:19:39 AM8/6/04
to
>
> 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?

Mark Hubbart

unread,
Aug 6, 2004, 11:32:32 AM8/6/04
to

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

> Gavin Sinclair (gsin...@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"

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

Ara.T.Howard

unread,
Aug 6, 2004, 11:39:13 AM8/6/04
to
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.

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.

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.

Caio Chassot

unread,
Aug 6, 2004, 11:43:40 AM8/6/04
to
> 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.

David A. Black

unread,
Aug 6, 2004, 12:32:31 PM8/6/04
to
Hi --

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

> 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.
>
> 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.

Ara.T.Howard

unread,
Aug 6, 2004, 2:55:44 PM8/6/04
to
On Sat, 7 Aug 2004, David A. Black wrote:

> 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.

i've always assumed this to be true too, but:

jib:~ > ruby a.rb 8192

---
-
Symbol:
max: "0.0039439201354981"
avg: "0.0000033703981899"
min: "0.0000019073486328"
-
String:
max: "0.0023880004882812"
avg: "0.0000032874231692"
min: "0.0000019073486328"


jib:~ > cat a.rb

require 'tempfile'
require 'tmpdir'
require 'fileutils'
require 'yaml'

class HashProfiler
PATHS = {
String => File.join(Dir.tmpdir, 'string'),
Symbol => File.join(Dir.tmpdir, 'symbol'),
}
at_exit{ PATHS.map{|t,p| FileUtils.rm_f p} }
class << self
def stats
list = []
PATHS.each do |type,path|
times = IO.readlines(path).map{|line| Float line}
avg = times.inject(0.0){|a,f| a += f} / times.size.to_f
list << Hash[
type.to_s => {
'min' => ('%16.16f' % times.min),
'max' => ('%16.16f' % times.max),
'avg' => ('%16.16f' % avg),
}
]
end
list
end
end
def initialize type, n
@type = type
@n = n
populate
gen_lookups
end
def populate
@h = {}
@n.times do |i|
key = "foobar#{ i }"
@h[(String == @type ? key : key.intern)] = rand
end
@keys = @h.keys
@size = @keys.size
end
def gen_lookups
@lookups = []
@n.times{|i| @lookups << @keys[rand(@size)]}
end
def profile
fork do
GC.disable
open(PATHS[@type],'a+') do |f|
@lookups.each do |k|
a = Time.now.to_f
v = @h[k]
b = Time.now.to_f
t = b - a
f.puts t
end
end
exit!
end
Process.wait
end
end


STDOUT.sync = true

n = Integer(ARGV.shift || 2 ** 13)

[String, Symbol].each do |type|
profiler = HashProfiler.new type, n
4.times{ profiler.profile }
end

y HashProfiler.stats


this suprises me - they look to be about the same. perhaps my code has a bug.

Joel VanderWerf

unread,
Aug 6, 2004, 3:37:30 PM8/6/04
to
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!

Mark Hubbart

unread,
Aug 6, 2004, 4:09:27 PM8/6/04
to

On Aug 6, 2004, at 8:41 AM, Ara.T.Howard wrote:

> 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.

This was mentioned elsewhere on this thread, I believe:

%w(abc hi\ there)
==>["abc", "hi there"]

> and you could never need
>
> hash['hi there'.intern]
>
> since you could not type
>
> hash[:hi there]

hash[:"hi there"]

I've used symbols with embedded spaces before, in hashes. It seems odd
to use them in method names, but as someone else pointed out, it does
work...

cheers,
Mark

0 new messages