Setting attributes with composition

The Dragon class is composed of the Room class and the Traits class.
The Room class inherits from the Location class which has attribute
accessors.

From the “creatures.rb” file, how can the attributes for a particular
Dragon object be set or read, please?

If the attributes are directly in the Dragon class (or its superclass,
Creature) then the attributes can be read fine.

[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ ruby creatures.rb
creatures.rb:14: undefined local variable or method `creaturs’ for
main:Object (NameError)
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Creature.rb
require ‘Math’
require ‘Traits’
require ‘Room’

class Creature

Creature.extend Math
include Math

def initialize ()
@location = Room.new
@traits = Traits.new
end

def saysHello ()
print self.class
puts " says to fuck off"
end

def inspect ()
print “class\t\t”
print self.class

    @location.inspect
    @traits.inspect

    print "\n"

end

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Location.rb
class Location

attr_accessor :x, :y

def initialize ()
@x = 0
@y = 0
end

def inspect ()
print “\nx\t\t”
print x
print “\ny\t\t”
print y
end

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Room.rb
require ‘Location’

class Room < Location

def initialize ()
@x = 0
@y = 0
end

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Location.rb
class Location

attr_accessor :x, :y

def initialize ()
@x = 0
@y = 0
end

def inspect ()
print “\nx\t\t”
print x
print “\ny\t\t”
print y
end

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Dragon.rb
require ‘Creature’

class Dragon < Creature

def initialize ()
super
end

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ date
Mon Nov 12 22:17:55 PST 2007
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $

I seem to have omitted the particular ruby file which directly
generated the error:

[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ ruby creatures.rb
creatures.rb:14: undefined local variable or method `creaturs’ for
main:Object (NameError)
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat creatures.rb
require ‘ArrayOfCreatures’
require ‘MakeCreature’
require ‘Location’

include MakeCreature

NumOfCreatures=2
creatures = ArrayOfCreatures.instance

NumOfCreatures.times do |i|
creatures[i]=Dragon.new
end

creaturs[0].location.x = 3
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ date
Mon Nov 12 22:28:42 PST 2007
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $

-Thufir

creaturs[0].location.x = 3

all i can see here, without too much analysis of your code, is that you
use creaturs[0] instead of creatures[0]

Yeah, spellink error corrected (thanks). I want to be able to set the
location for a dragon with something like:

drgn=Dragon.new
drgn.location(3,4)

However, I don’t want a “location” method added to Dragon nor its
superclass of Creature. That is, how is the location for drgn
changed? The exact syntax isn’t so important.

I seem to have lost meaningful output from the inspect method, seems
to be the object id :frowning:

[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ ruby creatures.rb
#Dragon:0xb7bf031c
#Dragon:0xb7bf0308
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat creatures.rb
require ‘ArrayOfCreatures’
require ‘MakeCreature’
require ‘Location’

include MakeCreature

NumOfCreatures=2
creatures = ArrayOfCreatures.instance

NumOfCreatures.times do |i|
creatures[i]=Dragon.new
end

creatures[0].location.x = 3

puts creatures
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Creature.rb
require ‘Math’
require ‘Traits’
require ‘Room’

class Creature

Creature.extend Math
include Math

attr_accessor :location

def initialize ()
@location = Room.new
@traits = Traits.new
end

def saysHello ()
print self.class
puts " says to fuck off"
end

def inspect ()
print “class\t\t”
print self.class

    @location.inspect
    @traits.inspect

    print "\n"

end

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Dragon.rb
require ‘Creature’

class Dragon < Creature

def initialize ()
super
end

def inspect ()
super
end

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Location.rb
class Location

attr_accessor :x, :y

def initialize ()
@x = 0
@y = 0
end

def inspect ()
print “\nx\t\t”
print x
print “\ny\t\t”
print y
end

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Room.rb
require ‘Location’

class Room < Location

def initialize ()
super
end

def inspect ()
super
end

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ cat Traits.rb
class Traits

end
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $ date
Tue Nov 13 02:06:30 PST 2007
[email protected] ~/rubyCode/creatures5 $
[email protected] ~/rubyCode/creatures5 $

thanks,

Thufir