%w for symbols

Discussion in 'Ruby' started by Caio Chassot, Aug 6, 2004.

  1. Caio Chassot

    Caio Chassot Guest

    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?)
    Caio Chassot, Aug 6, 2004
    #1
    1. Advertising

  2. 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?)




    %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
    Simon Strandgaard, Aug 6, 2004
    #2
    1. Advertising

  3. Caio Chassot

    Caio Chassot Guest

    > %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
    Caio Chassot, Aug 6, 2004
    #3
  4. Caio Chassot <> 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?

    martin
    Martin DeMello, Aug 6, 2004
    #4
  5. Caio Chassot

    Caio Chassot Guest

    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.
    Caio Chassot, Aug 6, 2004
    #5
  6. On Fri, 6 Aug 2004 16:00:32 +0900, 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?)


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

    -austin
    --
    Austin Ziegler *
    * Alternate:
    Austin Ziegler, Aug 6, 2004
    #6
  7. 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
    Florian Gross, Aug 6, 2004
    #7
  8. On Friday, August 6, 2004, 9:18:12 PM, Austin wrote:

    > On Fri, 6 Aug 2004 16:00:32 +0900, 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?)


    > 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, Aug 6, 2004
    #8
  9. 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
    Gavin Sinclair, Aug 6, 2004
    #9
  10. On Fri, 6 Aug 2004 20:21:25 +0900, Florian Gross <> 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.

    -austin
    --
    Austin Ziegler *
    * Alternate:
    Austin Ziegler, Aug 6, 2004
    #10
  11. Caio Chassot

    Eric Hodel Guest

    --GTZ+2qEBTXdGs1w1
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    Gavin Sinclair () wrote:

    > On Friday, August 6, 2004, 9:18:12 PM, Austin wrote:
    >=20
    > > On Fri, 6 Aug 2004 16:00:32 +0900, Caio Chassot <> wrote:
    > >> 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 - - 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--
    Eric Hodel, Aug 6, 2004
    #11
  12. Caio Chassot

    Ara.T.Howard Guest

    On Fri, 6 Aug 2004, Austin Ziegler wrote:

    > On Fri, 6 Aug 2004 20:21:25 +0900, Florian Gross <> 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
    ===============================================================================
    Ara.T.Howard, Aug 6, 2004
    #12
  13. Hi --

    On Fri, 6 Aug 2004, Eric Hodel wrote:

    > Gavin Sinclair () wrote:
    >
    > > On Friday, August 6, 2004, 9:18:12 PM, Austin wrote:
    > >
    > > > On Fri, 6 Aug 2004 16:00:32 +0900, 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?)

    > >
    > > > 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
    David A. Black, Aug 6, 2004
    #13
  14. Caio Chassot

    Caio Chassot Guest

    Eric Hodel wrote:
    >
    > %s is already used:
    >


    %i then? (intern)
    Caio Chassot, Aug 6, 2004
    #14
  15. Caio Chassot

    Caio Chassot Guest

    >
    > 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?
    Caio Chassot, Aug 6, 2004
    #15
  16. Caio Chassot

    Mark Hubbart Guest

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

    > Gavin Sinclair () wrote:
    >
    >> On Friday, August 6, 2004, 9:18:12 PM, Austin wrote:
    >>
    >>> On Fri, 6 Aug 2004 16:00:32 +0900, 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?)

    >>
    >>> 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
    Mark Hubbart, Aug 6, 2004
    #16
  17. Caio Chassot

    Ara.T.Howard Guest

    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.

    -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
    ===============================================================================
    Ara.T.Howard, Aug 6, 2004
    #17
  18. Caio Chassot

    Caio Chassot Guest

    > 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.
    Caio Chassot, Aug 6, 2004
    #18
  19. 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.


    David

    --
    David A. Black
    David A. Black, Aug 6, 2004
    #19
  20. Caio Chassot

    Ara.T.Howard Guest

    strings vs. symbols [WAS] Re: %w for symbols

    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.

    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
    ===============================================================================
    Ara.T.Howard, Aug 6, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QUs=?=

    Debug symbols not for all .aspx forms.

    =?Utf-8?B?QUs=?=, Jan 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    893
    Habib Heydarian [MSFT]
    Jan 23, 2004
  2. Gibs

    Displaying Special Symbols

    Gibs, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    455
    Ken Cox [Microsoft MVP]
    Jun 29, 2004
  3. Brian Henry

    <% symbols

    Brian Henry, Jul 6, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    574
    clintonG
    Jul 6, 2004
  4. Shawn South
    Replies:
    0
    Views:
    3,412
    Shawn South
    Aug 16, 2004
  5. Peter Blum

    Re: allow all currency symbols

    Peter Blum, Oct 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    460
    magister
    Nov 3, 2004
Loading...

Share This Page