Forum: Ruby SafeObject protection for standard gotcha's.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D812408537ac3a0fa2fec96eb8811559?d=identicon&s=25 John Carter (johncarter)
on 2006-05-29 03:39
(Received via mailing list)
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 Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter@tait.co.nz
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.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-05-30 23:15
(Received via mailing list)
On May 28, 2006, at 6:36 PM, John Carter 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 Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
This topic is locked and can not be replied to.