to_sym <=> to_str

Discussion in 'Ruby' started by trans. (T. Onoma), Nov 30, 2004.

  1. If String has #to_sym wouldn't it make sense for Symbol to have #to_str ?

    T.
     
    trans. (T. Onoma), Nov 30, 2004
    #1
    1. Advertising

  2. trans.  (T. Onoma)

    Sam Roberts Guest

    Quoteing , on Tue, Nov 30, 2004 at 11:17:26AM +0900:
    > If String has #to_sym wouldn't it make sense for Symbol to have #to_str ?


    I think the relationship would be:

    String#to_sym, ..#to_s, ..#to_i, #to_a

    are all at the same level.

    They are all methods on types that can be converted to the target, but
    are not ALREADY of the type of the target of the conversion.

    The next level is:

    #to_str, #to_ary, #to_int all are supported by classes that ARE of that
    type (in the duck-type sense).

    That list could include #to_symbol... except there is nothing in ruby
    that IS a symbol, only symbols are symbols.

    My way of looking at it anyhow...

    Though I'm guilty of writing a #to_integer, and adding it to both String
    and Integer... A string can be a binary representation of a octet
    sequence which has a representation as an unsigned integer. Since crypto
    apis inputs and outputs are "technically" integers, usually, but in
    actually always are treated as opaque blobs of data (String, in ruby), I
    wanted a way to 'duck-type' String and Integer to be the same, at least
    for my purposes.

    Maybe its bad, but it sure is useful.

    Sam
     
    Sam Roberts, Nov 30, 2004
    #2
    1. Advertising

  3. On Tuesday, November 30, 2004, 1:43:42 PM, Sam wrote:

    > Quoteing , on Tue, Nov 30, 2004 at 11:17:26AM +0900:
    >> If String has #to_sym wouldn't it make sense for Symbol to have #to_str ?


    > I think the relationship would be:


    > String#to_sym, ..#to_s, ..#to_i, #to_a


    > are all at the same level.


    > They are all methods on types that can be converted to the target, but
    > are not ALREADY of the type of the target of the conversion.


    > The next level is:


    > #to_str, #to_ary, #to_int all are supported by classes that ARE of that
    > type (in the duck-type sense).


    > That list could include #to_symbol... except there is nothing in ruby
    > that IS a symbol, only symbols are symbols.


    > My way of looking at it anyhow...


    I think that in a sense, a symbol IS a string -- an immutable one.
    Implicit conversion via Symbol#to_str makes sense to me.

    Gavin
     
    Gavin Sinclair, Nov 30, 2004
    #3
    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. Gavin Sinclair

    Difference between to_s and to_str

    Gavin Sinclair, Apr 5, 2004, in forum: Ruby
    Replies:
    6
    Views:
    154
    Yukihiro Matsumoto
    Apr 5, 2004
  2. robertj

    String.to_sym?

    robertj, Dec 12, 2005, in forum: Ruby
    Replies:
    4
    Views:
    116
    Trans
    Dec 13, 2005
  3. Dean
    Replies:
    0
    Views:
    228
  4. Ian Hunter

    String.intern vs String.to_sym

    Ian Hunter, Jul 28, 2008, in forum: Ruby
    Replies:
    2
    Views:
    194
    Stephen Celis
    Jul 28, 2008
  5. Patrick Doyle
    Replies:
    1
    Views:
    160
    Patrick Doyle
    Sep 30, 2008
Loading...

Share This Page