lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,
This is my first post to the lua mailing list.
I have just started learning lua.
Please just check the following code:
****************
ClassA = {}
function ClassA:new(name)
	object = {}
	setmetatable(object, self)
	self.__index = self
	self.name = name
	return object
end

ClassB = {}
function ClassB:new()
	object = {}
        setmetatable(object, self)
	self.__index = self
	self.tabs = {}
	return object
end
function ClassB:add(name)
	local ser = ClassA:new(name)
	table.insert(self.tabs,ser)
end
function ClassB:print()
	for i,obj in pairs(self.tabs) do
		print(obj.name)
	end
end

ob = ClassB:new()
ob:add('Earth')
ob:add('Mars')
ob:print()
****************
Shouldn't the above code print:
Earth
Mars
But it doesn't. It prints
Mars
Mars

What mistake am I making?
Thanks for your help.