<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br><br></div> <br><p class="airmail_on" style="color:#000;">On 15 July 2015 at 15:58:53, Duncan Stuart (<a href="mailto:dgmstuart@gmail.com">dgmstuart@gmail.com</a>) wrote:</p> <blockquote type="cite" class="clean_bq"><span><div><div></div><div>
<title></title>
<div dir="ltr">Thanks Frederick and Rob<br>
<br>
Defining to_date doesn't suffice: the <=> method on Date
doesn't try and coerce the argument into a date, which is
why Date.today > Time.now fails, even though Time responds
to to_date.<br>
<br>
If I were to define a coercion method, I think it would have to be
on Date, which feels wrong.</div>
<div class="gmail_extra"><br></div></div></div></span></blockquote><div><br></div><div>As a quick hack:</div><div><br></div><div><div>class NoExpectedDate</div><div> def to_s(format=:default)</div><div> "Unknown"</div><div> end</div><div> def coerce(other)</div><div> [other, 10000000] # a date long in the future</div><div> end</div><div>end</div></div><div><br></div><div><div>Date.today <=> NoExpectedDate.new</div><div>=> -1</div></div><div><br></div><div><br></div><div>Fred</div><div><br></div><div><br></div><br><blockquote type="cite" class="clean_bq"><span><div><div><div class="gmail_extra">
<div class="gmail_quote">On 15 July 2015 at 13:21, Rob Isenberg
<span dir="ltr"><<a href="mailto:robisenberg@me.com" target="_blank">robisenberg@me.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="auto">
<div>Hi Duncan, if you want your class to be comparable with other
built-in objects, I think you need to implement any #to_* methods
that make sense.</div>
<div><br></div>
<div>I think you need to write a #to_date method which with then
automatically be coerced when the comparison is done starting with
a Date object. </div>
<div><br>
Sent from my iPhone</div>
<div>
<div class="h5">
<div><br>
On 15 Jul 2015, at 13:04, Duncan Stuart <<a href="mailto:dgmstuart@gmail.com" target="_blank">dgmstuart@gmail.com</a>> wrote:<br>
<br></div>
<blockquote type="cite">
<div>
<div dir="ltr">Sorry that last code snippet should be:<br>
<p><span style="font-size:12.8000001907349px">def
sort_by_expected_date<br></span><span style="font-size:12.8000001907349px"> sort do |a,
b| <br></span><span style="font-size:12.8000001907349px"> if b.class =
NoExpectedDate <br></span><span style="font-size:12.8000001907349px">
1<br></span><span style="font-size:12.8000001907349px">
else<br></span><span style="font-size:12.8000001907349px"> a <=>
b<br></span><span style="font-size:12.8000001907349px">
end<br></span><span style="font-size:12.8000001907349px"> end<br></span><span style="font-size:12.8000001907349px">end</span></p>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On 15 July 2015 at 13:03, Duncan Stuart
<span dir="ltr"><<a href="mailto:dgmstuart@gmail.com" target="_blank">dgmstuart@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Hi LRUG - hopefully an interesting little
problem:<br>
<br>
I have an Event class which has an "expected_date" attribute.<br>
Some events don't have an expected date, so like a good little OO
programmer i've created a Null object:<br>
<br>
<div>class NoExpectedDate</div>
<div> def to_s(format=:default)</div>
<div> "Unknown"</div>
<div> end</div>
<div>end</div>
<div><br></div>
<div>This works great for printing the values, but when it comes to
sorting the list I of course get:<br>
ArgumentError: comparison of Date with NoExpectedDate
failed <br>
<br>
If I include Comparable and define <=> then one comparison
works, but the other doesn't :<br>
<br>
<div>class NoExpectedDate</div>
<div> include Comparable</div>
<div> def to_s(format=:default)</div>
<div> "Unknown"</div>
<div> end</div>
<div> def <=>(other_date)</div>
<div> 1 # Treat it as after every other date</div>
<div> end</div>
<div>end</div>
</div>
<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>
I suppose I could always just define a method on Event to do this
particular sort, but that seems nasty for all sorts of
reasons:<br>
<br></p>
<p>def sort_by_expected_date sort do |a, b| </p>
<p> if b.class = NoExpectedDate </p>
<p> 1</p>
<p> else</p>
<p> a <=> b</p>
<p> end</p>
<p> end</p>
<p>end</p>
<p><br></p>
</div>
</div>
</blockquote>
</div>
<br></div>
</div>
</blockquote>
</div>
</div>
<blockquote type="cite">
<div><span class=""><span>_______________________________________________</span><br>
<span>Chat mailing list</span><br>
<span><a href="mailto:Chat@lists.lrug.org" target="_blank">Chat@lists.lrug.org</a></span><br>
<span>Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a></span><br>
<span>Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a></span><br>
<span>List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a></span><br>
</span></div>
</blockquote>
</div>
<br>
_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>
Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>
List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
<br></blockquote>
</div>
<br></div>
_______________________________________________
<br>Chat mailing list
<br>Chat@lists.lrug.org
<br>Archives: http://lists.lrug.org/pipermail/chat-lrug.org
<br>Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
<br>List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
<br></div></div></span></blockquote></body></html>