Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Thing
- @@num_things = 0
- attr_reader( :name, :description )
- attr_writer( :description )
- def initialize( aName, aDescription )
- @name = aName
- @description = aDescription
- @@num_things +=1
- end
- def to_s
- return "(Thing.to_s):: The #{@name} thin is #{@description}"
- end
- def show_classvars
- return "There are #{@@num_things} thing objects in this game"
- end
- end
- class Room < Thing
- end
- class Treasure < Thing
- attr_reader :value
- attr_writer :value
- def initialize( aName, aDescription, aValue)
- super( aName, aDescription )
- @value = aValue
- end
- end
- class Map
- def initialize( someRooms )
- @rooms = someRooms
- end
- def to_s
- @rooms.each {
- |a_room|
- puts(a_room)
- }
- end
- end
- t1 = Treasure.new("sword", "an elvish weapon forged of gold",800)
- t2 = Treasure.new("dragon horde", "a hige pile of jewels", 550)
- room1 = Room.new("crystal grotto", "a glittery cavern")
- room2 = Room.new("dark cave", "a gloomy hole in the rocks")
- room3 = Room.new("forest glade", "a verdant clearing filled woth shimmering light")
- mymap = Map.new([room1,room2,room3])
- puts "\nlet's inspect the treasures..."
- puts "this is the treasure1: #{t1.inspect}"
- puts "this is the treasure2: #{t2.inspect}"
- puts "\nlet's try out the Thing.to_s method..."
- puts "yup, treasure 2 is #{t2.to_s}"
- puts "\nNow let's see how our attribute accessors work"
- puts "we'll evaluate this:"
- puts 't1 name= #{t1.name}, description= #{t1.description}, value= #{t1.value}'
- puts "t1 name= #{t1.name}, description= #{t1.description}, value= #{t1.value}"
- puts "\nNow we'll assign 100 to t1.value and alter t1.description..."
- t1.value = 100
- t1.description << " (now somewhat tarnished)"
- puts "t1 (NOW) name=#{t1.name}, description=#{t1.description}, value=#{t1.value}"
- puts "\nLet's take a look at room1..."
- puts "room1 name=#{room1.name}, description=#{room1.description}"
- puts "\nAnd the map..."
- puts "mymap = #{mymap.to_s}"
- puts "\nFinally, let's check how many Thing objects we've created..."
- puts( t1.show_classvars )
Add Comment
Please, Sign In to add comment