Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > to_sym <=> to_str

Reply
Thread Tools

to_sym <=> to_str

 
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      11-30-2004
If String has #to_sym wouldn't it make sense for Symbol to have #to_str ?

T.


 
Reply With Quote
 
 
 
 
Sam Roberts
Guest
Posts: n/a
 
      11-30-2004
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



 
Reply With Quote
 
 
 
 
Gavin Sinclair
Guest
Posts: n/a
 
      11-30-2004
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
rake: undefined method `to_sym' for [:first_name, :last_name]:Array Patrick Doyle Ruby 1 09-30-2008 07:02 PM
String.intern vs String.to_sym Ian Hunter Ruby 2 07-28-2008 10:23 PM
Does YAML::Syck::MergeKey have to_sym? Dean Ruby 0 12-26-2006 06:47 AM
String.to_sym? robertj Ruby 4 12-13-2005 02:39 PM
Difference between to_s and to_str Gavin Sinclair Ruby 6 04-05-2004 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57