Document your code and specify the method takes objects that act like Time, or
something like that, and then use the methods of Time you need to use. If they
violate your documented contract, it will cause exceptions, and they’ll know
they did something wrong.
At least, that’s the theoretical idea. More or less, the Ruby way of duck
typing is to assume you’re given a duck and treat it as such. You don’t try
to verify that it’s a duck, because then you’re doing static typing, and
there’s already exceptions if the correct methods aren’t there (and there
are ways of defining methods that you can’t check for explicitly, like #method_missing).
Wrote Dan Doel djd15@po.cwru.edu, on Mon, Apr 19, 2004 at 09:55:43AM +0900:
Just use it as you would a Time object.
That doesn’t work for me, if you look at my example, I need to know if
it’s a Date or a Time, and I need to treat them differently. But, I
would like to support any object which is “date-like” or “time-like”.
DateTime, in ruby 1.8, for example, is actually time-like (it has hours,
mins, secs).
I can see if it’s string-like or array-like by checking for #to_str or #to_ary, but is there a general case?
Maybe I’ll just write a function that checks for the year/mon/day
methods and hour/min/sec methods, to distinguish.
Yes, I suppose you would want to check for methods explicitly if you need to
treat them differently. That’s probably the most efficient way that leaves the
question of the actuall class open.
All the methods I can think of in the standard library that do different
things based on what they’re passed, though, actually check the class of
the object (I think).
Another option would be to do what I said, and split your method into
multiple methods, like:
def string_foo…
def date_foo…
def time_foo…
But the result probably wouldn’t be pretty.
Cheers.
Dan
Wrote Dan Doel djd15@po.cwru.edu, on Mon, Apr 19, 2004 at 09:55:43AM
+0900:
···
On Monday 19 April 2004 3:06 pm, Sam Roberts wrote:
Just use it as you would a Time object.
That doesn’t work for me, if you look at my example, I need to know if
it’s a Date or a Time, and I need to treat them differently. But, I
would like to support any object which is “date-like” or “time-like”.
DateTime, in ruby 1.8, for example, is actually time-like (it has hours,
mins, secs).
I can see if it’s string-like or array-like by checking for #to_str or #to_ary, but is there a general case?
Maybe I’ll just write a function that checks for the year/mon/day
methods and hour/min/sec methods, to distinguish.
Quoteing djd15@po.cwru.edu, on Tue, Apr 20, 2004 at 05:22:23AM +0900:
All the methods I can think of in the standard library that do different
things based on what they’re passed, though, actually check the class of
the object (I think).
I was reminded by this in a recent post, which said the ruby stdlib had
been changed to use the presence of :read as a method to indicate an
object is IO-like. In that case, the categories I’m aware of are:
Class respond_to?
String :to_str
Array :to_ary
IO :read
Symbol :id2name (or is this silly? are only Symbols symbol-like?)
Are there other “signature” methods?
Would be useful on the wiki page, maybe under ruby-idioms?
I use some other ones in my code:
xxx.respond_to? :each
xxx.respond_to? :call
The first one is typically an Enumerable (an existing ruby module)
The second one is typically a Callable (Smalltalkish I think, nothing eqv
in Ruby).
It may prove a good idea to encapsulate this in some methods (of Object)
class Object
def stringable? ; respond_to? :to_str end
def arrayable? ; respond_to? :to_ary end
def readable? ; respond_to? :read end
def enumerable? ; respond_to? :each end
def callable? ; respond_to? :call end
end
But I suppose this is rather questionable?
Another option is to mixim the an appropriate module,
as is already the case for Enumerable.
Yours,
Jean-Hugues
···
At 06:44 26/04/2004 +0900, you wrote:
Quoteing djd15@po.cwru.edu, on Tue, Apr 20, 2004 at 05:22:23AM +0900:
All the methods I can think of in the standard library that do different
things based on what they’re passed, though, actually check the class of
the object (I think).
I was reminded by this in a recent post, which said the ruby stdlib had
been changed to use the presence of :read as a method to indicate an
object is IO-like. In that case, the categories I’m aware of are:
Class respond_to?
String :to_str
Array :to_ary
IO :read
Symbol :id2name (or is this silly? are only Symbols symbol-like?)
Are there other “signature” methods?
Would be useful on the wiki page, maybe under ruby-idioms?