Hi --

On Sun, 23 Apr 2006, Greg Coit wrote:

> Hi all,
>
> Trying hard to get my brain wrapped around the concepts of OO (using
> Ruby which i'm also new to but doesn't seem as foreign to me).  I'm
> using rrobots (at rubyforge) to practice...
>
> This is what's provided to me:
>
> #state:
> #       team
> #       battlefield_height
> #       battlefield_width
> #       energy
> #       gun_heading
> #       gun_heat
> #       heading
> #       size
> #       radar_heading
> #       time
> #       game_over
> #       speed
> #       x
> #       y
> #actions:
> #       accelerate
> #       stop
> #       fire
> #       turn
> #       turn_gun
> #       turn_radar
> #       broadcast
> #       say
> #events:
> #       broadcast
> #       got_hit
> #       robot_scanned
>
> Now I've started to write a bot serveral times, but I'm thinking I need
> to back up a bit and figure out who to approach this from an OO
> standpoint.
>
> Ruby reinforces the idea of passing messages to objects (right?).  My instinct
> is to write methods named turn.left, turn.right, sleep.up, speed.down,
> gun.fire, radar.sweep, etc....

The question you should ask is: is there an entity called a "turn" in
the universe of your application -- the domain -- which can perform an
action, or which has a property, called "left"?  I would say the
answer is no.  A turn might have a "direction" property, and that
might be "left".  But turn.left suggests that you're telling a turn to
left, or to show you its left, neither of which really fits.

(It's true that you'll see libraries that do things like:
turn.five.degrees.to.the.left, but I consider that serious dot-abuse
and would stay away from it.)

So, you're likely to see things like:

   robot.turn :direction => "left", :degrees => 5

where direction and degrees are actually hash keys that can be parsed
out in the turn method and used to modify state (x and y, I would
assume).

Anyway, for me, the main thing is that every message that goes to an
object should make sense either as a command to the object, or as a
request for information from the object.


David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!