# Object available in one class but not another?

**URL:** <https://www.ruby-forum.com/t/object-available-in-one-class-but-not-another/94263>\
**Category:** Ruby\
**Created:** [April 27, 2006, 9:26am UTC](https://www.ruby-forum.com/t/object-available-in-one-class-but-not-another/94263 "2006-04-27T09:26:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snow-templar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@snow-templar](https://www.ruby-forum.com/u/snow-templar)\
**Post date:** [April 27, 2006, 9:26am UTC](https://www.ruby-forum.com/t/object-available-in-one-class-but-not-another/94263/1 "2006-04-27T09:26:22Z")

</div>

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:in `target?’

Here’s my code:

[http://rafb.net/paste/results/RtkFqy50.html](http://rafb.net/paste/results/RtkFqy50.html)

Thanks for any and all help.

Greg

---

<div class="post-metadata">

**Author:** ![snow-templar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@snow-templar](https://www.ruby-forum.com/u/snow-templar)\
**Post date:** [April 28, 2006, 12:30am UTC](https://www.ruby-forum.com/t/object-available-in-one-class-but-not-another/94263/2 "2006-04-28T00:30:53Z")

</div>

On 4/27/06, Dave B. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![snow-templar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@snow-templar](https://www.ruby-forum.com/u/snow-templar)\
**Post date:** [April 27, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/object-available-in-one-class-but-not-another/94263/3 "2006-04-27T13:31:54Z")

</div>

Greg C. wrote:

> Playing around more with rrobots.  
> …  
> Here’s my code:
> 
> [http://rafb.net/paste/results/RtkFqy50.html](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
