Symbols and strings

Discussion in 'Ruby' started by maghac, Aug 14, 2007.

  1. maghac

    maghac Guest

    Hi,
    as a long-term perl user just recently converted to ruby, I'm curious
    about one particular syntax "feature": ruby symbols and how they
    relate to strings.

    Isn't really :name a shortcut for "name"? (I read somewhere in an
    explanation of attr_reader that e.g :age was the "name" of the
    variable age, while age is the actual content of that variable). If
    so, couldn't you use this in hash keys as well, e.g say hashvar[:key]
    instead of hashvar['key'].

    Are there situations where you cannot use symbols instead of strings,
    or the other way around?

    I might be too used to the way strings and barewords are handled in
    perl (if something looks like a string, and it's not a function call,
    it's interpreted as a string. This means you can say $hashvar{key}
    without quoting the key).
     
    maghac, Aug 14, 2007
    #1
    1. Advertising

  2. maghac

    Robert Dober Guest

    On 8/14/07, maghac <> wrote:
    > Hi,
    > as a long-term perl user just recently converted to ruby, I'm curious
    > about one particular syntax "feature": ruby symbols and how they
    > relate to strings.
    >
    > Isn't really :name a shortcut for "name"? (I read somewhere in an
    > explanation of attr_reader that e.g :age was the "name" of the
    > variable age, while age is the actual content of that variable). If
    > so, couldn't you use this in hash keys as well, e.g say hashvar[:key]
    > instead of hashvar['key'].
    >
    > Are there situations where you cannot use symbols instead of strings,
    > or the other way around?
    >
    > I might be too used to the way strings and barewords are handled in
    > perl (if something looks like a string, and it's not a function call,
    > it's interpreted as a string. This means you can say $hashvar{key}
    > without quoting the key).

    This question is asked very frequently, please search the archives for
    more info.
    I will try to give a very quick answer nevertheless:

    Symbols are immutable, thus great to represent immutable data, often
    that makes them an excellent choice for hash keys
    { :the_value => 42, :alternatively => 22, :eek:r_even => 101010 }
    they just do not play the role of Strings, coming from Perl you just
    had to use Strings, you did not have a tool for names (wait a moment
    was there not such a thing as references to constants?).
    Very roughly put I see Symbols as name, and Strings as data, whenever
    I can use Symbols I use them, comes natural after some time.

    HTH
    Robert
    --
    [...] as simple as possible, but no simpler.
    -- Attributed to Albert Einstein
     
    Robert Dober, Aug 14, 2007
    #2
    1. Advertising

  3. Alle marted=EC 14 agosto 2007, maghac ha scritto:
    > Hi,
    > as a long-term perl user just recently converted to ruby, I'm curious
    > about one particular syntax "feature": ruby symbols and how they
    > relate to strings.
    >
    > Isn't really :name a shortcut for "name"? (I read somewhere in an
    > explanation of attr_reader that e.g :age was the "name" of the
    > variable age, while age is the actual content of that variable). If
    > so, couldn't you use this in hash keys as well, e.g say hashvar[:key]
    > instead of hashvar['key'].
    >
    > Are there situations where you cannot use symbols instead of strings,
    > or the other way around?
    >
    > I might be too used to the way strings and barewords are handled in
    > perl (if something looks like a string, and it's not a function call,
    > it's interpreted as a string. This means you can say $hashvar{key}
    > without quoting the key).


    Symbols and strings are two different things. There are several situations =
    in=20
    which you can pass a method either a symbol or a string, but this happens=20
    because who wrote the method took into account both possibilities. For=20
    instance, attr_reader and similar, send, instance_variable_get and=20
    instance_variable_set, define_method can take both a string or a symbol.=20
    Module#const_get, instead, only accepts a symbol.

    You can see that string and symbols are different using hashes:

    h =3D { :a =3D> 1}
    puts h[:a]
    =3D> 1
    puts h['a']
    =3D> nil
    h['a'] =3D 2
    puts h['a']
    =3D> 2
    puts h[:a]
    =3D> 1

    There have been several threads on this topic. I suggest you look at them.

    I hope this helps

    Stefano
     
    Stefano Crocco, Aug 14, 2007
    #3
  4. maghac

    maghac Guest

    On Aug 14, 11:11 am, "Robert Dober" <> wrote:
    > On 8/14/07, maghac <> wrote:
    >
    > > Hi,
    > > as a long-term perl user just recently converted to ruby, I'm curious
    > > about one particular syntax "feature": ruby symbols and how they
    > > relate to strings.

    >
    > > Isn't really :name a shortcut for "name"? (I read somewhere in an
    > > explanation of attr_reader that e.g :age was the "name" of the
    > > variable age, while age is the actual content of that variable). If
    > > so, couldn't you use this in hash keys as well, e.g say hashvar[:key]
    > > instead of hashvar['key'].

    >
    > > Are there situations where you cannot use symbols instead of strings,
    > > or the other way around?

    >
    > > I might be too used to the way strings and barewords are handled in
    > > perl (if something looks like a string, and it's not a function call,
    > > it's interpreted as a string. This means you can say $hashvar{key}
    > > without quoting the key).

    >
    > This question is asked very frequently, please search the archives for
    > more info.
    > I will try to give a very quick answer nevertheless:
    >
    > Symbols are immutable, thus great to represent immutable data, often
    > that makes them an excellent choice for hash keys
    > { :the_value => 42, :alternatively => 22, :eek:r_even => 101010 }
    > they just do not play the role of Strings, coming from Perl you just
    > had to use Strings, you did not have a tool for names (wait a moment
    > was there not such a thing as references to constants?).
    > Very roughly put I see Symbols as name, and Strings as data, whenever
    > I can use Symbols I use them, comes natural after some time.
    >
    > HTH
    > Robert
    > --
    > [...] as simple as possible, but no simpler.
    > -- Attributed to Albert Einstein


    Thanks, that cleared a few things for me.

    I found something in the FAQ about it, but it didn't really answer my
    question on the differences/similiarities between symbols and strings.

    thanks
    Magnus
     
    maghac, Aug 14, 2007
    #4
  5. maghac

    Tim Hunter Guest

    maghac wrote:
    > On Aug 14, 11:11 am, "Robert Dober" <> wrote:
    >
    >> On 8/14/07, maghac <> wrote:
    >>
    >>
    >>> Hi,
    >>> as a long-term perl user just recently converted to ruby, I'm curious
    >>> about one particular syntax "feature": ruby symbols and how they
    >>> relate to strings.
    >>>
    >>> Isn't really :name a shortcut for "name"? (I read somewhere in an
    >>> explanation of attr_reader that e.g :age was the "name" of the
    >>> variable age, while age is the actual content of that variable). If
    >>> so, couldn't you use this in hash keys as well, e.g say hashvar[:key]
    >>> instead of hashvar['key'].
    >>>
    >>> Are there situations where you cannot use symbols instead of strings,
    >>> or the other way around?
    >>>
    >>> I might be too used to the way strings and barewords are handled in
    >>> perl (if something looks like a string, and it's not a function call,
    >>> it's interpreted as a string. This means you can say $hashvar{key}
    >>> without quoting the key).
    >>>

    >> This question is asked very frequently, please search the archives for
    >> more info.
    >> I will try to give a very quick answer nevertheless:
    >>
    >> Symbols are immutable, thus great to represent immutable data, often
    >> that makes them an excellent choice for hash keys
    >> { :the_value => 42, :alternatively => 22, :eek:r_even => 101010 }
    >> they just do not play the role of Strings, coming from Perl you just
    >> had to use Strings, you did not have a tool for names (wait a moment
    >> was there not such a thing as references to constants?).
    >> Very roughly put I see Symbols as name, and Strings as data, whenever
    >> I can use Symbols I use them, comes natural after some time.
    >>
    >> HTH
    >> Robert
    >> --
    >> [...] as simple as possible, but no simpler.
    >> -- Attributed to Albert Einstein
    >>

    >
    > Thanks, that cleared a few things for me.
    >
    > I found something in the FAQ about it, but it didn't really answer my
    > question on the differences/similiarities between symbols and strings.
    >
    > thanks
    > Magnus
    >
    >
    >

    Don't make more out of this than there is. A symbol is a kind of literal
    in Ruby. It's the name of a number. You pick the name, Ruby picks the
    number. Ruby guarantees that whenever you use the symbol in your program
    it will always refer to the same number.

    That's it. That's all there is. Really.

    Given a symbol, you can retrieve its name in the form of a string with
    the #to_s method. You can convert a string (or almost any sequence of
    characters, for that matter) to a symbol with the #to_sym method. Most
    methods that accept symbol arguments, like attr_reader and kin, accept
    strings as well. All attr_reader does with your symbol is use its name
    as the name of the attribute.

    --
    RMagick OS X Installer [http://rubyforge.org/projects/rmagick/]
    RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618]
    RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html]
     
    Tim Hunter, Aug 14, 2007
    #5
  6. maghac

    Phlip Guest

    Tim Hunter wrote:

    > Don't make more out of this than there is. A symbol is a kind of literal
    > in Ruby. It's the name of a number. You pick the name, Ruby picks the
    > number. Ruby guarantees that whenever you use the symbol in your program
    > it will always refer to the same number.


    :foo.object_id == :foo.object_id

    So if you add a method to one :foo, can anyone who invokes :foo access
    the method?

    --
    Phlip
    http://www.oreilly.com/catalog/9780596510657/
    ^ assert_xpath
    http://tinyurl.com/yrc77g <-- assert_latest Model
     
    Phlip, Aug 14, 2007
    #6
  7. maghac

    Guest

    Hi --

    On Tue, 14 Aug 2007, Phlip wrote:

    > Tim Hunter wrote:
    >
    >> Don't make more out of this than there is. A symbol is a kind of literal
    >> in Ruby. It's the name of a number. You pick the name, Ruby picks the
    >> number. Ruby guarantees that whenever you use the symbol in your program
    >> it will always refer to the same number.

    >
    > :foo.object_id == :foo.object_id
    >
    > So if you add a method to one :foo, can anyone who invokes :foo access
    > the method?


    $ ruby -ve 'class << :abc; end'
    ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]
    -e:1: no virtual class for Symbol (TypeError)


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
     
    , Aug 14, 2007
    #7
    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. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  2. Daniel Berger
    Replies:
    2
    Views:
    456
    Park Heesob
    Nov 28, 2003
  3. Joe Van Dyk

    symbols vs strings vs ?

    Joe Van Dyk, Feb 3, 2005, in forum: Ruby
    Replies:
    2
    Views:
    99
    Joe Van Dyk
    Feb 3, 2005
  4. Brian Candler

    Symbols and frozen strings

    Brian Candler, Sep 6, 2007, in forum: Ruby
    Replies:
    12
    Views:
    215
    Daniel DeLorme
    Sep 7, 2007
  5. shenry
    Replies:
    14
    Views:
    258
    Rick DeNatale
    Nov 3, 2009
Loading...

Share This Page