[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: to_str behavior

Stefan Rusterholz

8/14/2007 12:38:00 AM

Nasir Khan wrote:
> Hi,
> I have two related questions on to_str behavior. AFAIK to_str does
> automatic
> coercion to string wherever one is required, it is to be provided for
> objects that exhibit string like behavior. But this mental model did not
> match up with my test -

class StringLike
include Comparable
def initialize(val)
@val = val
end
def <=>(other)
@val <=> other.to_str
end
def to_str
@val
end
end

x = StringLike.new("hi")
x == "hi" # => true
"hi" == x # => true

That's from one of my older experiments. I don't remember the reasons.
Feel free to examine why it works with <=> + Comparable.
HTH.

Regards
Stefan
--
Posted via http://www.ruby-....

2 Answers

David A. Black

8/14/2007 3:25:00 AM

0

David A. Black

8/15/2007 12:58:00 AM

0