Szukaj…


Uwagi

Podstawowym wzorem pisania modułu jest wypełnienie tabeli kluczami, które są nazwami funkcji i wartościami, które same są funkcjami. Następnie moduł zwraca tę funkcję w celu wywołania require kodu i użycia go. (Funkcje są pierwszorzędnymi wartościami w Lua, więc przechowywanie funkcji w tabeli jest łatwe i powszechne.) Tabela może również zawierać dowolne ważne stałe w postaci, powiedzmy, ciągów lub liczb.

Pisanie modułu

--- 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
}

Alternatywnym podejściem do powyższego jest utworzenie tabeli najwyższego poziomu, a następnie zapisanie funkcji bezpośrednio w niej. W tym języku nasz moduł powyżej wyglądałby tak:

-- 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

Z punktu widzenia dzwoniącego różnica między tymi dwoma stylami jest niewielka. (Jedną z różnic, o których warto wspomnieć, jest to, że pierwszy styl utrudnia użytkownikom monkeypatch modułu. Jest to albo profesjonalista, albo oszustwo, w zależności od twojego punktu widzenia. Aby uzyskać więcej informacji na ten temat, zobacz ten post na blogu autorstwa Enrique García Cota.)

Korzystanie z modułu

-- 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
Licencjonowany na podstawie CC BY-SA 3.0
Nie związany z Stack Overflow