ComputerCraft Archive

class

computer utility kepler155c github

Description

From http://lua-users.org/wiki/SimpleLuaClasses

Installation

Copy one of these commands into your ComputerCraft terminal:

wget:wget https://raw.githubusercontent.com/kepler155c/opus/develop-1.8/sys/modules/opus/class.lua class
Archive:wget https://cc.shobie.xyz/cc/get/gh-kepler155c-opus-sys-modules-opus-class class
Quick Install: wget https://cc.shobie.xyz/cc/get/gh-kepler155c-opus-sys-modules-opus-class class

Usage

Run: class

Tags

none

Source

View Original Source

Code Preview

-- From http://lua-users.org/wiki/SimpleLuaClasses
-- (with some modifications)

-- class.lua
-- Compatible with Lua 5.1 (not 5.0).
return function(base)
	local c = { }    -- a new class instance
	if type(base) == 'table' then
		-- our new class is a shallow copy of the base class!
		if base._preload then
			base = base._preload(base)
		end
		for i,v in pairs(base) do
			c[i] = v
		end
		c._base = base
	end
	-- the class will be the metatable for all its objects,
	-- and they will look up their methods in it.
	c.__index = c

	-- expose a constructor which can be called by <classname>(<args>)
	setmetatable(c, {
		__call = function(class_tbl, ...)
			local obj = { }
			setmetatable(obj,c)
			if class_tbl.init then
				class_tbl.init(obj, ...)
			else
				-- make sure that any stuff from the base class is initialized!
				if base and base.init then
					base.init(obj, ...)
				end
			end
			return obj
		end
	})

	c.is_a =
		function(self, klass)
			local m = getmetatable(self)
			while m do
				if m == klass then return true end
				m = m._base
			end
			return false
		end
	return c
end