Now that I've finally worked out how class inheritance works in ruby, I'm designing an inventory system for a text rpg. There are some quirks that I can't figure out, however.
Item class:
class Item
attr_accessor :name, :type, :attack, :armor, :wearloc, :weight, :price
def initialize(name, type, attack, armor, wearloc, weight, price)
@name = name
@type = type
@attack = attack
@armor = armor
@wearloc = wearloc
@weight = weight
@price = price
end
end
Inventory class:
class Inventory
attr_accessor :items
def initialize
@@items = []
end
def add_item(item)
@@items << item
end
attr_accessor :inventory
def inventory
@@items.each do |item|
puts "#{item.name} (#{item.type})"
if item.attack != nil ; puts " %-20s %00d" % ['Attack', item.attack] ; end
if item.armor != nil ; puts " %-20s %00d" % ['Armor', item.armor] ; end
if item.wearloc != nil ; puts " %-20s %00s" % ['Wear', item.wearloc] ; end
if item.weight != nil ; puts " %-20s %00d" % ['Weight', item.weight] ; end
if item.price != nil ; puts " %-20s %00d" % ['Price', item.price] ; end
end
end
end
Player class:
class Player < Inventory
attr_accessor :playername
def initialize(playername)
@playername = playername
end
end
Instantiating the inventory, creating a few items, adding them to the inventory all works.
inv = Inventory.new
broadsword = Item.new('a heavy broadsword', 'weapon', 5, nil, 'wield', 15, 2)
breastplate = Item.new('a mithril breastplate', 'armor', nil, 10, 'torso', 15, 5)
ring = Item.new('a gold ring', 'armor', nil, 3, 'finger', 2, 20)
inv.add_item(broadsword)
inv.add_item(breastplate)
inv.add_item(ring)
However, when I call the inventory method from the Player class, I get the desired output, plus the 3 #item objects like this:
a heavy broadsword (weapon)
Attack 5
Wear wield
Weight 15
Price 2
a mithril breastplate (armor)
Armor 10
Wear torso
Weight 15
Price 5
a gold ring (armor)
Armor 3
Wear finger
Weight 2
Price 20
#<Item:0x007fae1b8b11d8>
#<Item:0x007fae1b8b1138>
#<Item:0x007fae1b8b1098>
Why are those last three lines there? I can't figure out where it is coming from or how to fix it.
Update:
Reworked for Item < Inventory < Player inheritance with instance vars instead of class vars.
class Player
attr_accessor :playername
def initialize(playername)
@playername = playername
end
end
class Inventory < Player
attr_accessor :items
def initialize
@items = []
end
def add_item(item)
@items << item
end
attr_accessor :inventory
def inventory
@items.each do |item|
puts "#{item.name} (#{item.type})"
if item.attack != nil ; puts " %-20s %00d" % ['Attack', item.attack] ; end
if item.armor != nil ; puts " %-20s %00d" % ['Armor', item.armor] ; end
if item.wearloc != nil ; puts " %-20s %00s" % ['Wear', item.wearloc] ; end
if item.weight != nil ; puts " %-20s %00d" % ['Weight', item.weight] ; end
if item.price != nil ; puts " %-20s %00d" % ['Price', item.price] ; end
end
nil
end
end
class Item < Inventory
attr_accessor :name, :type, :attack, :armor, :wearloc, :weight, :price
def initialize(name, type, attack, armor, wearloc, weight, price)
@name = name
@type = type
@attack = attack
@armor = armor
@wearloc = wearloc
@weight = weight
@price = price
end
end
inv = Inventory.new
broadsword = Item.new('a heavy broadsword', 'weapon', 5, nil, 'wield', 15, 2)
breastplate = Item.new('a mithril breastplate', 'armor', nil, 10, 'torso', 15, 5)
ring = Item.new('a gold ring', 'armor', nil, 3, 'finger', 2, 20)
inv.add_item(broadsword)
inv.add_item(breastplate)
inv.add_item(ring)
player = Player.new('Chris')
# puts player.inventory
puts inv.inventory