I have just noticed that my codeline has silent breakage all over the
place and I have an idea how to catch this sort of thing in future…
How it happened is this…
I had a variable “platform” that I used all over the place. It was a
String.
Then it had to become substantially smarter, so I made it an object
instance of class Platform.
Unfortunately in quite a few places somehow I forgot to convert
if platform == ‘thisone’
to
if platform.name == ‘thisone’
(or even better and simpler just…
platform.doTheRightThing
)
And…
Ruby silently accepted my mistake, threw no error and took the false
branch.
So to catch that class of breakage I should do something like…
module PolymorphicallySafeObject
def ==(object)
raise “Strict Typing failure expected #{self.class} found
#{object.class}” unless
object.kind_of?( self.class) || self.kind_of?( object.class)
end
end
class Platform < SomeThing
include PolymorphicallySafeObject
.
.
.
.
end
Now it occurs to me this might be quite a useful trick.
For example catching failures to define an equal method if you define a
hash method
and vice versa.
Question for the Group…
What other useful cross checks and gotchas catchers could I include in
SafeObject?
For extra marks, come up with a DuckSafeObject.
John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand
Carter’s Clarification of Murphy’s Law.
“Things only ever go right so that they may go more spectacularly wrong
later.”
From this principle, all of life and physics may be deduced.