<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Ruby can do that . . .</title>
	<atom:link href="http://www.rubygeek.com/2006/03/04/ruby-can-do-that/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rubygeek.com/2006/03/04/ruby-can-do-that/</link>
	<description>Ruby, PHP, Python and Perl</description>
	<pubDate>Tue, 06 Jan 2009 03:32:33 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Adz</title>
		<link>http://www.rubygeek.com/2006/03/04/ruby-can-do-that/#comment-18</link>
		<dc:creator>Adz</dc:creator>
		<pubDate>Thu, 22 Jun 2006 15:21:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubygeek.com/?p=43#comment-18</guid>
		<description>You can actually override the "==" operator:

def ==(other)
  (@value == other.value) &#38;&#38; (@suit == other.suit)
end

Although maybe you'd like to leave == for object identity comparison.... 

As to the "to_str" method... well, you'd use "to_s" which is what puts calls.  

Why? Well because puts is supposed to print objects of any type.  to_str would only exist in objects which are 'would-be-strings'.

It's a bit confusing, but these links may help:

http://www.ruby-forum.com/topic/59379
"to_str ....  is reserved for classes that are made to be used in place of
Strings, when they need to be converted to true Strings"

http://whytheluckystiff.net/articles/rubyOneEightOh.html
"With to_str, Matz is giving us a simpler way of demonstrating that our classes can be used as strings directly. Nearly all builtin methods use to_str to determine if an object is (or can be used as) a String. Think of it: if you extend the String class, then you have to write alternate methods (sub!, length, append, etc.) tailored to your needs.
...
So why not use to_s? Because to_s coerces objects into strings. So, to_str is an implicit cast, whereas to_s is an explicit cast.

Think of timestamps. We want to be able to easily convert a timestamp into a String for printing:

 puts "Time.now: " + Time.now.to_s
 #=&#62; Time.now: Mon Aug 04 13:37:43 MDT 2003

But we don’t want to load a File based on a timestamp:

 File.open( Time.now )
 #=&#62; TypeError: cannot convert Time into String
     from (irb):3:in `initialize'
     from (irb):3:in `open'
     from (irb):3

Generally, we just don’t need a timestamp to act as a String. So we use to_s to explicitly convert it."</description>
		<content:encoded><![CDATA[<p>You can actually override the &#8220;==&#8221; operator:</p>
<p>def ==(other)<br />
  (@value == other.value) &amp;&amp; (@suit == other.suit)<br />
end</p>
<p>Although maybe you&#8217;d like to leave == for object identity comparison&#8230;. </p>
<p>As to the &#8220;to_str&#8221; method&#8230; well, you&#8217;d use &#8220;to_s&#8221; which is what puts calls.  </p>
<p>Why? Well because puts is supposed to print objects of any type.  to_str would only exist in objects which are &#8216;would-be-strings&#8217;.</p>
<p>It&#8217;s a bit confusing, but these links may help:</p>
<p><a href="http://www.ruby-forum.com/topic/59379" rel="nofollow">http://www.ruby-forum.com/topic/59379</a><br />
&#8220;to_str &#8230;.  is reserved for classes that are made to be used in place of<br />
Strings, when they need to be converted to true Strings&#8221;</p>
<p><a href="http://whytheluckystiff.net/articles/rubyOneEightOh.html" rel="nofollow">http://whytheluckystiff.net/articles/rubyOneEightOh.html</a><br />
&#8220;With to_str, Matz is giving us a simpler way of demonstrating that our classes can be used as strings directly. Nearly all builtin methods use to_str to determine if an object is (or can be used as) a String. Think of it: if you extend the String class, then you have to write alternate methods (sub!, length, append, etc.) tailored to your needs.<br />
&#8230;<br />
So why not use to_s? Because to_s coerces objects into strings. So, to_str is an implicit cast, whereas to_s is an explicit cast.</p>
<p>Think of timestamps. We want to be able to easily convert a timestamp into a String for printing:</p>
<p> puts &#8220;Time.now: &#8221; + Time.now.to_s<br />
 #=&gt; Time.now: Mon Aug 04 13:37:43 MDT 2003</p>
<p>But we don’t want to load a File based on a timestamp:</p>
<p> File.open( Time.now )<br />
 #=&gt; TypeError: cannot convert Time into String<br />
     from (irb):3:in `initialize&#8217;<br />
     from (irb):3:in `open&#8217;<br />
     from (irb):3</p>
<p>Generally, we just don’t need a timestamp to act as a String. So we use to_s to explicitly convert it.&#8221;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
