Zoeken…


Opmerkingen

Het basispatroon voor het schrijven van een module is het vullen van een tabel met toetsen die functienamen zijn en waarden die de functies zelf zijn. De module keert dan terug deze functie voor het bellen code te require en te gebruiken. (Functies zijn eersteklas waarden in Lua, dus het opslaan van een functie in een tabel is eenvoudig en gebruikelijk.) De tabel kan ook belangrijke constanten bevatten in de vorm van bijvoorbeeld tekenreeksen of getallen.

Module schrijven

--- trim: a string-trimming module for Lua
-- Author, date, perhaps a nice license too
--
-- The code here is taken or adapted from  material in
-- Programming in Lua, 3rd ed., Roberto Ierusalimschy

-- trim_all(string) => return string with white space trimmed on both sides 
local trim_all = function (s)
  return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

-- trim_left(string) => return string with white space trimmed on left side only
local trim_left = function (s)
  return (string.gsub(s, "^%s*(.*)$", "%1"))
end

-- trim_right(string) => return string with white space trimmed on right side only
local trim_right = function (s)
  return (string.gsub(s, "^(.-)%s*$", "%1"))
end

-- Return a table containing the functions created by this module
return {
  trim_all = trim_all,
  trim_left = trim_left,
  trim_right = trim_right
}

Een alternatieve benadering voor de bovenstaande methode is om een tabel op het hoogste niveau te maken en de functies daar direct in op te slaan. In dat idioom zou onze bovenstaande module er als volgt uitzien:

-- A conventional name for the table that will hold our functions
local M = {}

-- M.trim_all(string) => return string with white space trimmed on both sides
function M.trim_all(s)
  return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

-- M.trim_left(string) => return string with white space trimmed on left side only
function M.trim_left(s)
  return (string.gsub(s, "^%s*(.*)$", "%1"))
end

-- trim_right(string) => return string with white space trimmed on right side only
function M.trim_right(s)
  return (string.gsub(s, "^(.-)%s*$", "%1"))
end


return M

Vanuit het oogpunt van de beller is er weinig verschil tussen de twee stijlen. (Een verschil dat het vermelden waard is, is dat de eerste stijl het voor gebruikers moeilijker maakt om de module monkeypatch. Dit is een pro of een con, afhankelijk van uw standpunt. Zie voor meer informatie dit blogbericht van Enrique García Cota.)

Gebruik van de module

-- The following assumes that trim module is installed or in the caller's package.path,
-- which is a built-in variable that Lua uses to determine where to look for modules.
local trim = require "trim"

local msg = "    Hello, world!      "
local cleaned = trim.trim_all(msg)
local cleaned_right = trim.trim_right(msg)
local cleaned_left = trim.trim_left(msg)

-- It's also easy to alias functions to shorter names.
local trimr = trim.trim_right
local triml = trim.trim_left


Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow