Guest User

Untitled

a guest
Jun 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.01 KB | None | 0 0
  1. class Thing
  2.   @@num_things = 0
  3.  
  4.   attr_reader( :name, :description )
  5.   attr_writer( :description )
  6.  
  7.   def initialize( aName, aDescription )
  8.     @name = aName
  9.     @description = aDescription
  10.     @@num_things +=1
  11.   end
  12.  
  13.   def to_s
  14.     return "(Thing.to_s):: The #{@name} thin is #{@description}"
  15.   end
  16.  
  17.   def show_classvars
  18.     return "There are #{@@num_things} thing objects in this game"
  19.   end
  20. end
  21.  
  22. class Room < Thing
  23. end
  24.  
  25. class Treasure < Thing
  26.   attr_reader :value
  27.   attr_writer :value
  28.  
  29.   def initialize( aName, aDescription, aValue)
  30.     super( aName, aDescription )
  31.     @value = aValue
  32.   end
  33. end
  34.  
  35. class Map
  36.  
  37.  
  38.   def initialize( someRooms )
  39.     @rooms = someRooms
  40.   end
  41.  
  42.   def to_s
  43.     @rooms.each {
  44.       |a_room|
  45.       puts(a_room)
  46.     }
  47.   end
  48. end
  49.  
  50. t1 = Treasure.new("sword", "an elvish weapon forged of gold",800)
  51. t2 = Treasure.new("dragon horde", "a hige pile of jewels", 550)
  52.  
  53. room1 = Room.new("crystal grotto", "a glittery cavern")
  54. room2 = Room.new("dark cave", "a gloomy hole in the rocks")
  55. room3 = Room.new("forest glade", "a verdant clearing filled woth shimmering light")
  56. mymap = Map.new([room1,room2,room3])
  57.  
  58. puts "\nlet's inspect the treasures..."
  59. puts "this is the treasure1: #{t1.inspect}"
  60. puts "this is the treasure2: #{t2.inspect}"
  61. puts "\nlet's try out the Thing.to_s method..."
  62. puts "yup, treasure 2 is #{t2.to_s}"
  63. puts "\nNow let's see how our attribute accessors work"
  64. puts "we'll evaluate this:"
  65. puts 't1 name= #{t1.name}, description= #{t1.description}, value= #{t1.value}'
  66. puts "t1 name= #{t1.name}, description= #{t1.description}, value= #{t1.value}"
  67. puts "\nNow we'll assign 100 to t1.value and alter t1.description..."
  68. t1.value = 100
  69. t1.description << " (now somewhat tarnished)"
  70. puts "t1 (NOW) name=#{t1.name}, description=#{t1.description}, value=#{t1.value}"
  71. puts "\nLet's take a look at room1..."
  72. puts "room1 name=#{room1.name}, description=#{room1.description}"
  73. puts "\nAnd the map..."
  74. puts "mymap = #{mymap.to_s}"
  75. puts "\nFinally, let's check how many Thing objects we've created..."
  76. puts( t1.show_classvars )
Add Comment
Please, Sign In to add comment