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 $