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.
--
Simon Strandgaard
I guess that pretty much misses the point, otherwise I could just go
ahead and write [:a, :b, :c]
thanks anyway
are you trying to avoid creating the intermediate strings?
martin
Ideally, yes. But I'm just looking for convinience.
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
> 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
> 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
> And I think that [:foo, :bar, :qux] is already short enough.
And ['foo', 'bar', 'qux'] isn't?
Gavin
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.
%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
> 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
===============================================================================
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
%i then? (intern)
I guess the usual %m{abc hi\ there}
Or is that the escape syntax you're trying to avoid?
> 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
> 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.
I like it.
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.
> 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.
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!
> 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