Skip to content

Instantly share code, notes, and snippets.

@Bradshaw
Forked from hugodes/gist:6489552
Last active December 22, 2015 14:59

Revisions

  1. Bradshaw revised this gist Sep 8, 2013. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions gistfile1.lua
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    local class = {} -- Since you're returning this at the end, I assume you want it local to the file
    -- Files are like functions in lua, except they're only meant to be calles once so you're better off doing what I suggested in my gist

    -- Problem is, you can only have this file run once, or otherwise remove the "local" but then you're redefining the values each time you call the file (with an ugly dofile no less)

    local health -- This is local to the whole file

    function class.getHealth() -- Renamed to avoid confusing, replaced ob:meth() with ob.meth() syntax because that's not how closures are used
    local health = 100 -- This is local to the whole file
    -- class wasn't useful
    function getHealth()
    return health -- This returns the value, so health is closed to the file, but can be "read" with this function
    end

    return class

  2. Bradshaw revised this gist Sep 8, 2013. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions gistfile1.lua
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    class = {}
    local class = {} -- Since you're returning this at the end, I assume you want it local to the file

    local health
    -- Problem is, you can only have this file run once, or otherwise remove the "local" but then you're redefining the values each time you call the file (with an ugly dofile no less)

    function class:health()
    return health
    local health -- This is local to the whole file

    function class.getHealth() -- Renamed to avoid confusing, replaced ob:meth() with ob.meth() syntax because that's not how closures are used
    return health -- This returns the value, so health is closed to the file, but can be "read" with this function
    end

    return class
  3. @hugodes hugodes created this gist Sep 8, 2013.
    9 changes: 9 additions & 0 deletions gistfile1.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    class = {}

    local health

    function class:health()
    return health
    end

    return class