# SafeObject protection for standard gotcha's

**URL:** <https://www.ruby-forum.com/t/safeobject-protection-for-standard-gotchas/61863>\
**Category:** Ruby\
**Created:** [May 29, 2006, 3:39am UTC](https://www.ruby-forum.com/t/safeobject-protection-for-standard-gotchas/61863 "2006-05-29T03:39:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [May 29, 2006, 3:39am UTC](https://www.ruby-forum.com/t/safeobject-protection-for-standard-gotchas/61863/1 "2006-05-29T03:39:42Z")

</div>

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 : removed\_email\_address@domain.invalid  
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.

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [May 30, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/safeobject-protection-for-standard-gotchas/61863/2 "2006-05-30T23:15:39Z")

</div>

On May 28, 2006, at 6:36 PM, John C. wrote:

> And…  
> {object.class}" unless  
> object.kind\_of?( self.class) || self.kind\_of?( object.class)  
> end  
> end

a) unit tests would have caught this breakage for you.  
b) If you’re going to be comparing objects it is best to define #==  
(or #\<=\> and include Comparable).

class Platform  
def ==(o)  
case o  
when String then  
warn “comparing Platform with String on #{caller[1]}” # or raise  
name == o  
else  
name == o.name  
end  
end  
end

> # 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.

There will always be one you didn’t think of. Instead of adding  
these kinds of after-the-fact checks write unit tests. They provide  
more complete coverage with less time spent tracking down all your  
errors.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
