class Formatter {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
public static String show(String s) { return s; }
public static String show(Date d) { return sdf.format(d); }
public static String show(double x) { return x < 0.05 ? '<0.1' : "" + (Math.round(x * 10) / 10.0); }
public static String show(Object o) { return o.toString(); }
}
To invoke, I would use this:
Formatter.show(myObject);
Java will select the method based on the class I send. Note that there is a method for object to catch anything unexpected.
In Ruby, I would approach this quite differently. There is no need for a new class, just modify the existing classes. This is not possible in Java; I could extend
Date
, but I would have to ensure that every date I sent was of my date class. String
and float
cannot be extended at all.class String
def show
to_s
end
end
# Remember the require 'date.rb'
class DateTime
DATE_FORMAT = '%d/%b/%y'
def show
strftime(DATE_FORMAT)
end
end
class Float
def show
self < 0.05 ? '<0.1' : (self * 10).round / 10.0
end
end
class Object
def show
to_s
end
end
That is more verbose, but the result is much neater, much more object-orientated, as now the method can be invoked like this:
my_object.show
I have a library of useful Java methods. It is a collection of static methods that do various operations on arrays and strings. In Ruby, I am building up a library that changes how arrays and string behave. Just occasionally, that is not the best way - again because of duck-typing, as it happens. Say I have a method to format dates consistently. All it does is invoke
strftime
with a certain format string. I could define my method as part of the DateTime
class, however I would then be unable to use it with Time objects. In this case, I am better adding the method to the Object class, then DateTime, Date and Time objects would all be able to use it, as they all have strftime
methods (and duck-typing allows this to work).
No comments:
Post a Comment