Forum: Ruby object available in one class but not another?

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.
4db4d1d1782fe68a945963682b43dd3d?d=identicon&s=25 Greg Coit (Guest)
on 2006-04-27 09:26
(Received via mailing list)
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

Thanks for any and all help.

Greg
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-04-27 13:31
(Received via mailing list)
Greg Coit 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
4db4d1d1782fe68a945963682b43dd3d?d=identicon&s=25 Greg Coit (Guest)
on 2006-04-28 00:30
(Received via mailing list)
On 4/27/06, Dave Burt <dave@burt.id.au> 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
This topic is locked and can not be replied to.