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 »Alcon - ActionScript Logging Console
July 25, 2005 – 10:30 pmAlcon is a lightweight external output console for Flash ActionScript that allows developers to view debugging information without relying on the Flash IDE. SWF files can be debugged and monitored ...
More fun about GTA and Co
July 25, 2005 – 4:06 amMore funny stuff from the GTA front, check out www.videogamesareevil.com! Aah I love it when people get cynical! And if you are up to more fun in the same direction, ...
Stuff on my cat!
July 21, 2005 – 11:01 amCats are just cool! They are most independent from us humans but sometimes they let us get closer to them. If that is happening, it is also sometimes possible ...
Have time will travel!
July 19, 2005 – 4:25 pmFound this classified a while ago in a BBS signature ... ... And damn I thought that's the guy I saw last month when I traveled back into 1629 by modifying ...
Library Items Renamer And Convert All To Clips
July 11, 2005 – 10:27 pmDue to popular demand I've put these two JSFL scripts back up again. They are useful for renaming and organizing items in Flash' Library Panel. Library Items Renamer v1.2 With LIR you ...
Review: GT Advance 3 Pro Concept Racing (Game Boy Advance)
August 1, 2004 – 6:26 amWe've been to her majesty's sisters house in Koga the last weekend which is around 1h from Tokyo and spent a lazy Saturday/Sunday there and mostly played games on ...
Until the pad buttons smoke!
June 21, 2004 – 2:11 pmMore Shoot'em'up goodness can be found at Shmups!, a website dedicated to 2D Shooter games with plenty of reviews and information from Katakis on the C64 over Z-Out on Amiga ...
Graphical User Interfaces - When will they be normalized?
June 12, 2004 – 12:50 pmI've installed SuSE Linux v9.1 a couple of days ago on my now secondary PC and after 2-3 days also managed to get rid of that ugly default KDE desktop ...
The 8 and 16-Bit Revival lives!
June 11, 2004 – 6:17 amI know what you think! And you are right! I've been absent for a while again from here but there are reasons for it. I'm working on a bigger ...
Space smugglers hobby room
May 8, 2004 – 5:59 amWe've been to the Star Wars Science and Art exhibition in Ueno yesterday. It was held in the Basement halls of the National Science Museum and I can tell it ...
| Filed under »