[rspec-users] [rails] An authorization question

Phlip phlip2005 at gmail.com
Mon Mar 2 14:57:38 EST 2009


James Byrne wrote:

> Is this a semantic confusion on my part?  Should I consider that what I 
> do with x.exists? is tell the object to answer a question?

Consider...

   if x.exists?
     x.important_method()
   else
     # nothing!
   end

Now lets upgrade the variable x. Sometimes it points to a thing that exists, and 
sometimes it points to a stub object whose only job is to return 'false' from 
exists?, and do nothing inside important_method():

   if x.exists?
     x.important_method()
   else
     x.important_method()
   end

Now you can take out the if!

   x.important_method()

We are no longer asking x if it exists. We are telling it what to do, and 
letting it decide how to do it. That is the heart of OO programming.

(There is still an 'if', somewhere - the one that populates x. But - hopefully - 
it's only in one place.)

-- 
   Phlip
   http://www.zeroplayer.com/



More information about the rspec-users mailing list