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
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: to_str behavior
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password