<div dir="ltr">Hmm; previous attempt to send this seems to have failed. Think it might be my plussed subscription address.<br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 15, 2015 at 1:17 PM, Frederick Cheung <span dir="ltr"><<a href="mailto:frederick.cheung@gmail.com" target="_blank">frederick.cheung@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><div style="font-family:Helvetica,Arial;font-size:13px;color:rgb(0,0,0);margin:0px"><br></div> <br> <div></div> <br><p style="color:rgb(0,0,0)">On 15 July 2015 at 13:03:21, Duncan Stuart (<a href="mailto:dgmstuart@gmail.com" target="_blank">dgmstuart@gmail.com</a>) wrote:</p> <blockquote type="cite"><span><div><div></div><div>






<div dir="ltr"><div><p><span>$ </span><span>NoExpectedDate</span><span>.new ></span>
<span>Date</span><span>.today<br></span><span>=></span> <span>true</span></p>
<p><span>$ </span><span>Date</span><span>.today ></span> <span>NoExpectedDate</span><span>.new<br></span>ArgumentError: comparison of Date with
NoExpectedDate failed<br>
<br>
I think this is because Date's <=> method expects it's
argument to be a Date object or a number ("a numeric value as an
astronomical Julian day number"). I've tried defining to_i and to_r
on NoExpectedDate, but no dice.<br>
<br>
Can I get NoExpectedDate to pretend to be a Date (like
SimpleDelegator lies about it's class)? Is that evil?<br>
<br></p></div></div></div></div></span></blockquote><div><br></div></span><div>I think this is what coercion is for (e.g. <a href="http://www.mutuallyhuman.com/blog/2011/01/25/class-coercion-in-ruby/" target="_blank">http://www.mutuallyhuman.com/blog/2011/01/25/class-coercion-in-ruby/</a> )</div></div></blockquote><div><br></div><div class="gmail_extra" style="font-size:12.8000001907349px"><div class="gmail_quote"><div>Yep, #coerce is what you're looking for but it's not very clearly documented. The documentation for Date#<=> doesn't admit that it calls coerce, but that's what it does if it can't deal with the types of the arguments.</div><div><br></div><div>The implementation of #coerce is a bit tricky: the method is passed the other operand of whichever binary operator has been invoked, so calling 'Date.today > NoExpectedDate.new' will eventually result in NoExpectedDate#coerce being passed the value of Date.today. Here's an implementation that works:</div><div><br></div></div></div><blockquote style="font-size:12.8000001907349px;margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote">require 'date'</div></div><span class="im"><div class="gmail_extra"><div class="gmail_quote"><br></div></div><div class="gmail_extra"><div class="gmail_quote">class NoExpectedDate</div></div><div class="gmail_extra"><div class="gmail_quote">  include Comparable</div></div><div class="gmail_extra"><div class="gmail_quote"><br></div></div><div class="gmail_extra"><div class="gmail_quote">  def to_s(format=:default)</div></div><div class="gmail_extra"><div class="gmail_quote">    "Unknown"</div></div><div class="gmail_extra"><div class="gmail_quote">  end</div></div><div class="gmail_extra"><div class="gmail_quote"><br></div></div><div class="gmail_extra"><div class="gmail_quote">  def <=>(other_date)</div></div><div class="gmail_extra"><div class="gmail_quote">    1</div></div></span><div class="gmail_extra"><div class="gmail_quote">  end</div></div><div class="gmail_extra"><div class="gmail_quote"><br></div></div><div class="gmail_extra"><div class="gmail_quote">  def coerce(other)</div></div><div class="gmail_extra"><div class="gmail_quote">    case other</div></div><div class="gmail_extra"><div class="gmail_quote">    when Date</div></div><div class="gmail_extra"><div class="gmail_quote">      return other, Float::INFINITY</div></div><div class="gmail_extra"><div class="gmail_quote">    else</div></div><div class="gmail_extra"><div class="gmail_quote">      nil</div></div><div class="gmail_extra"><div class="gmail_quote">    end</div></div><div class="gmail_extra"><div class="gmail_quote">  end</div></div><div class="gmail_extra"><div class="gmail_quote">end</div></div><div class="gmail_extra"><div class="gmail_quote"><br></div></div><div class="gmail_extra"><div class="gmail_quote">puts NoExpectedDate.new > Date.today # true</div></div><div class="gmail_extra"><div class="gmail_quote">puts Date.today > NoExpectedDate.new # false</div></div><div class="gmail_extra"><div class="gmail_quote"><br></div></div><div class="gmail_extra"><div class="gmail_quote">l = [</div></div><div class="gmail_extra"><div class="gmail_quote">  Date.new(1789, 7, 14),</div></div><div class="gmail_extra"><div class="gmail_quote">  Date.new(1215, 6, 15),</div></div><div class="gmail_extra"><div class="gmail_quote">  NoExpectedDate.new,</div></div><div class="gmail_extra"><div class="gmail_quote">  Date.new(1066, 10, 14),</div></div><div class="gmail_extra"><div class="gmail_quote">]</div></div><div class="gmail_extra"><div class="gmail_quote"><br></div></div></blockquote></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><span style="font-size:12.8000001907349px">puts l.sort.join(', ') # 1066-10-14, 1215-06-15, 1789-07-14, Unknown</span></div></div></div></blockquote><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>-Mark.</div><div> </div></div><br></div></div>