Object available in one class but not another?


#1

Hi all,

Playing around more with rrobots.

Method tick in class GoofeyBot is called by module Robot (which is
“included” at the beginning of all 4 of my classes).

Robot provides (among many other things) an object called
radar_heading. I can print the return value of the object just fine
on line 112 (class GoofeyBot) but not at line 69 (class Radar)
(radar_heading returns null here and the next line, 70, generates:

NoMethodError: undefined method +' for nil:NilClass ./GoofeyDuck.rb:70:intarget?’

Here’s my code:

http://rafb.net/paste/results/RtkFqy50.html

Thanks for any and all help.

Greg


#2

On 4/27/06, Dave B. removed_email_address@domain.invalid wrote:

whereas the parent GoofeyDuck has a separate radar. You have 4 robots, 3
inside the other 1!)

Here’s an alternative skeleton, with only 1 robot per robot:

Dave,

You’re awesome, and 100% in the money. Thanks for taking the time to
do this, I’m going to fless out the skeleton you gave me right away!

Thanks!

Greg


#3

Greg C. wrote:

Playing around more with rrobots.

Here’s my code:

http://rafb.net/paste/results/RtkFqy50.html

Thanks for any and all help.

Hi Greg,

I’m going to ignore your question in hope that this advice is as useful
as an answer would be.

Don’t include Robot in Radar or Gun. That type of relationship is “a Gun
is a kind of Robot”, which is not what you want. GoofeyDuck is your
Robot.

(Your problem is probably related to the fact that, for example,
radar_heading inside a Radar object refers to that Radar’s radar,
whereas the parent GoofeyDuck has a separate radar. You have 4 robots, 3
inside the other 1!)

Here’s an alternative skeleton, with only 1 robot per robot:

class GoofeyDuck
include Robot

def initialize
@radar = Radar.new(self)
@gun = Gun.new(self)
@tank = Tank.new(self)
end

This defines a class named GoofeyDuck::Radar.

You can refer to it as Gun inside GoofeyDuck’s definition.

class Radar
def initialize(robot)
@robot = robot
end
def target?
puts @robot.radar_heading
@robot.radar_heading + (@scan / 2)
end
#…
end

class Tank
def initialize(robot)
@robot = robot
end
#…
end

class Gun
def initialize(robot)
@robot = robot
end
#…
end
end

Cheers,
Dave