Customizing toString()
By sascha, September 8, 2008 – 2:15 am -When writing classes for a framework I usually put a custom toString method into important classes so that debugging becomes easier. Normally they would go like something along the lines of:
override public function toString():String { return "[ClassName]"; }
… Sometimes adding properties to the returned String that give back information about the class, e.g.
override public function toString():String { return "[ImageClass, size=" + _size + "]"; }
But writing them rigid like that is a disadvantage when you decide later to refactor class names. Admittedly it’s also not a very elegant way so I got the idea to take the class name that is returned by getQualifiedClassName(). The only problem is that getQualifiedClassName not only provides the type name but also the whole package String of the class. Regular Expressions to the rescue! After twiddling around with them for a while (I’m by no means a RegExp expert) I got my toString method into the shape that I desired:
override public function toString():String { return "[" + getQualifiedClassName(this).match("[^:]*$")[0] + ", size=" + _size + "]"; }
This way it matches the String returned by getQualifiedClassName with the Regular Expression [^:]*$ which checks from the right end for an arbitrary text up to the first occurring colon, but without including the colon. Taking the first element of the Array returned by match() and you got what you need!
A nice way of using this is when writing abstract classes that contain the toString method and any subclass can use that toString method without the need to override it … that is of course unless you want ot add other output information.
Featured
FEAT (Freelancer’s Estimation Assistance Tool)
FEAT (abbreviation for Freelancer’s Estimation Assistance Tool) is a tool to help freelancers calculate hourly rates and project pricing estimates. It is inspired by a very similar calculation PDF sheet which was created by Lauren of creativecurio.com and by some other useful freelancer pricing resources on the net.
The tool uses the same calculations like the ones from the links mentioned above plus it stores your values so you don’t have to enter them again every time. Version 1.0 features hourly rate calculation, a project pricing wizard and an option to change the visual theme of the tool. It also resides nicely on your desktop or wherever you put it and is there whenever you need to make an estimation.
Continue Reading »Thing on a spring
May 4, 2004 – 11:39 am... or things in the spring! I got my new rig set up finally! Almost done with installing all the stuff that I need for a convenient usage. This ...
European Vintage Computer Festival
April 28, 2004 – 8:22 pm"Let us return to those thrilling days of yesteryear, when geeks were geeks and floppies were really floppy." European Vintage Computer Festival
Kraftwerk - CCH, Hamburg (28.03.2004)
March 28, 2004 – 10:27 pmPart 1: Four elderly men (black suits, red shirts, black ties / videos in background) perform "The Man-Machine", "Expo 2000", "Tour de France 2003", "Vitamin", "Tour de France", "Autobahn", "The Model", ...
Hidden Videogame Pr00n
March 24, 2004 – 3:14 pmNow this is just hilarious ... Accidental Video Game Porn or like you would say 'don't let your mother see this!' An archive of screenshots and animations showing explicit (and ...
Graphical User Interfaces - When will they be substituted?
March 23, 2004 – 6:17 amThough GUIs did not really change over the past 20 years and seem to become a little bit outdated today everybody wonders what will be a better way of ...
Daily computer jumble
March 15, 2004 – 7:55 amThis was how I've felt when I entered the Ginza Apple Store some days ago! It was like "Hey everything is nice and easy and good" ... but in a strangely ...
Transpacific Hardwares
March 12, 2004 – 6:42 amIt's about time! We've finished a rather big job by now and it's time for some coddling! I'm still loitering around with my outdated AMD 1.2GHz with yesterdays ...
The Sims is not about simulating interpersonal relationships
March 11, 2004 – 5:08 amI've been playing The Sims several times and while it was a very interesting (and resource demanding) type of game at first, with every add-on that was released the intended ...
640 Kilobyte ought to be enough for anybody
March 10, 2004 – 4:33 am... was a quote from Bill Gates from 1981 according to the book Computers - An Illustrated History from Christian Wurster which I had a sneak look into some days ...
Lightspeed flight and laserfight
March 8, 2004 – 8:46 amReview: Sentinel Worlds 1: Future Magic (DOS/C64) One of my very favorite games back on Amiga was the space RPG Hard Nova (to be reviewed in a while) which took me ...
| Filed under »