New Posts  All Forums:Forum Nav:

OO in Lua help

post #1 of 2
Thread Starter 
I'm new to Lua and trying to wrap my head around everything being a table. I'm having a bit of trouble with a basic class I'm trying to create.

The class is a table of keys which have a table of sets. My problem area is with the add_dep function. I think I'm adding to my table but when I print it out I do not seem to see anything (like nothing was added).

Any help would be greatly appreciated.

Class:
Code:
-- Class for handling a table of dependenies
--
-- Table example:
--     { key1 = {dep1=true, dep2=true}, key2 = {dep2=true}, key3 = {dep2=true, dep3=true} }
--
--  Dependency table is a table of key={Set} values, or a table of a table of sets
--

-- Dependency class object
Dependency = {

    new = function (self)
        dep = {} -- create object
        setmetatable(dep, self)
        self.__index = self
        self.__newindex = self
        return dep
    end,

    have = function (self, key)
        for k in pairs(self) do
            if k == key then
                return true
            end
        end
        return false
    end,

    get_deps = function (self, key)
        for k,v in pairs(self) do
            if k == key then
                return v
            end
        end
        return nil
    end,

    add_dep = function (self, key, list)
        -- TODO add error checking and testing
        if self[key] == nil then
            self[key] = {}
        end
        for _,v in pairs(list) do
            print("adding " .. v)
            self[key][v] = true
        end
        return self
    end,

    print = function (self)
        print("Dependency Table --------")
        for k,v in pairs(self) do
            print(k)
            for dep in pairs(v) do
                print("  " .. dep)
            end
        end
    end,

}

return Dependency

Test script:
Code:
require 'dependency'

d = Dependency:new()
print(d:have("key1"))
print(d:have("test1"))
print(d:get_deps("test1"))
d:add_dep("key1",{"dep1", "dep2"})
d:print()

Output:
Code:
false
false
nil
adding dep1
adding dep2
Dependency Table --------

Expected:
Code:
Dependency Table --------
key1
   dep1
   dep2
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
post #2 of 2
Thread Starter 
Seems to be working now. Guess all I needed to do was post the question on here.

The change that I made was just removing a line from the new function, so it looks like this now:
Code:
new = function (self)
        dep = {} -- create object
        self.__index = self
        setmetatable(dep, self)
        return dep
    end,
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming