[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

to_sym <=> to_str

T. Onoma

11/30/2004 2:17:00 AM

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

T.


2 Answers

Sam Roberts

11/30/2004 2:44:00 AM

0

Quoteing transami@runbox.com, 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



Gavin Sinclair

11/30/2004 4:03:00 AM

0

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

> Quoteing transami@runbox.com, 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