User:Audiodude/modules/Example

--- Example module. -- @module example -- @alias p


local p = {} --All Lua modules on Wikipedia must begin by defining a variable

                   --that will hold their externally accessible functions.
                   --Such variables can have whatever name you want and may 
                   --also contain various data as well as functions.

--- Hello world function -- @param {table} frame current frame -- @return Hello world p.hello = function( frame ) --Add a function to "p".

                                       --Such functions are callable in Wikipedia
                                       --via the #invoke command.
                                       --"frame" will contain the data that Wikipedia
                                       --sends this function when it runs. 
                                -- 'Hello' is a name of your choice. The same name needs to be referred to when the module is used.
   
   local str = "Hello World!"  --Declare a local variable and set it equal to
                               --"Hello World!".  
   
   return str    --This tells us to quit this function and send the information in
                 --"str" back to Wikipedia.
   

end -- end of the function "hello"

--- Hello world function -- @param {table} frame current frame -- @param {string} frame.args[1] name -- @return Hello world function p.hello_to(frame) -- Add another function local name = frame.args[1] -- To access arguments passed to a module, use `frame.args` -- `frame.args[1]` refers to the first unnamed parameter -- given to the module return "Hello, " .. name .. "!" -- `..` concatenates strings. This will return a customized -- greeting depending on the name given, such as "Hello, Fred!" end

--- Counts fruit -- @param {table} frame current frame -- @param {string} frame.args.bananas number of bananas -- @param {string} frame.args.apples number of apples -- @return Number of apples and bananas function p.count_fruit(frame)

local num_bananas = tonumber(frame.args.bananas) or 0 -- Named arguments (I have 0 bananas and 0 apples) local num_apples = tonumber(frame.args.apples) or 0 -- are likewise accessed by indexing `frame.args` by name (`frame.args["bananas"]`, -- or equivalently `frame.args.bananas`.

local conj_bananas = num_bananas == 1 and 'banana' or 'bananas'

   local conj_apples = num_apples == 1 and 'apple' or 'apples'
   										-- Ternary operators assign values based on a condition in a compact way.

-- Here, `conj_bananas` gets `'banana'` if `num_bananas` is 1, else `'bananas'`. -- Similarly, `conj_apples` gets `'apple'` if `num_apples` is 1, else `'apples'`.

   return 'I have ' .. num_bananas ..  ' ' .. conj_bananas .. ' and ' .. num_apples .. ' ' .. conj_apples														

-- Like above, concatenate a bunch of strings together to produce -- a sentence based on the arguments given. end

--- Lucky function -- @param {string} a -- @param {string} b -- @return Whether a is lucky. return p --All modules end by returning the variable containing their functions to Wikipedia. -- Now we can use this module by calling Hello World!, -- Hello, foo !, or I have 5 bananas and 6 apples -- Note that the first part of the invoke is the name of the Module's wikipage, -- and the second part is the name of one of the functions attached to the -- variable that you returned.

-- The "print" function is not allowed in Wikipedia. All output is accomplished -- via strings "returned" to Wikipedia.

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.