From: Stefan Rusterholz Date: 2007-08-14T09:38:18+09:00 Subject: Re: to_str behavior 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-forum.com/.