%w for symbols

C

Caio Chassot

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?)
 
S

Simon Strandgaard

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

Caio Chassot

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

Martin DeMello

Caio Chassot said:
%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?

martin
 
C

Caio Chassot

Martin said:
%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.
 
A

Austin Ziegler

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{} ?

-austin
 
F

Florian Gross

Austin said:
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
 
G

Gavin Sinclair

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
 
A

Austin Ziegler

Austin said:
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.

-austin
 
E

Eric Hodel

--GTZ+2qEBTXdGs1w1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
On Friday, August 6, 2004, 9:18:12 PM, Austin wrote:
=20
Is there any similar notation to %w[ word word2 word3 ] that returns an
array of symbols instead of strings?
=20
If not, are there any plans for it in future ruby? Is it a good idea a= nyway?
=20
(Maybe that's a good example of where we could use macros?)
=20
It seems common enough that it might be nice to have this. Perhaps
%W{} instead of %w{} ?
=20
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"

--=20
Eric Hodel - (e-mail address removed) - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


--GTZ+2qEBTXdGs1w1
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)

iD8DBQFBE48sMypVHHlsnwQRAqoqAKDKyaOs8pDbuWk/g0m5hyB4Ed2NpwCg5Ysz
oZD2eGbnNtbnPaxKgX4IlgY=
=szrJ
-----END PGP SIGNATURE-----

--GTZ+2qEBTXdGs1w1--
 
A

Ara.T.Howard

Austin said:
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
===============================================================================
 
D

David A. Black

Hi --

Gavin said:
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
 
C

Caio Chassot

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?
 
M

Mark Hubbart

Gavin said:
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
 
A

Ara.T.Howard

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.

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

Caio Chassot

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

David A. Black

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
 
A

Ara.T.Howard

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.

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

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top