require and Module Values
Lesson 29 of 48 in Coddy's Lua for Ruby Developers course.
Ruby require reports whether a feature was newly loaded; Ruby modules also act as namespaces and mixins. Lua require returns the module's exported value, commonly a table, and caches it in package.loaded. A Lua module file normally defines local internals and returns its API table. Do not return nothing and assume the same import contract as Ruby. Sibling files are loaded by module name without the .lua suffix.
Ruby
first = require "set"
again = require "set"
puts first
puts againLua 5.1
local first=require("math")
local again=require("math")
print(first == again)
Porting trap
Lua require is not Ruby include: loading a module does not automatically mix its methods into another object.
Implement the required public API in the editable Lua modules. The visible locked main.lua loads them with require, supplies input/output, and checks specified module behavior. Keep the module names and return contracts.
Challenge
EasyInput is integer price and nonnegative quantity. Implement subtotal in pricing.lua and call it from app.lua. Return the product. Do not print during require.
Edit app.lua, pricing.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Public API checks: pricing.subtotal(price, quantity) must work independently of app.run, including zero quantity and signed price. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local pricing=require("pricing")
assert(pricing.subtotal(4,3)==12 and pricing.subtotal(4,0)==0 and pricing.subtotal(-2,3)==-6,"pricing.subtotal must implement the public product API")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Lua for Ruby Developers
1From Ruby to Lua
Run Lua Modulesputs to printRuby Locals and Lua ScopeRuby Integer to Lua NumberStrings as Byte SequencesRecap: Format a Receipt4Methods, Functions, Closures
Methods to FunctionsKeyword Defaults to OptionsTuples to Multiple ResultsBlocks and Captured LocalsRecap: Pricing Functions2Values and Decisions
nil Values and Missing FieldsShared Truth Ruleselsif to elseifRuby == and Table IdentityRecap: Admission Rules5Arrays and Hashes to Tables
Arrays to Dense TablesHash Records and pairsSet Membership with Keysdup and Table CopiesRecap: Count Inventory3Iteration and Flow
Ranges to Numeric forwhile, until, and repeateach_with_index to ipairsEnumerable to Explicit LoopsRecap: Reading Stats6Ordering, Modules, and Errors
sort to table.sortRegex to Lua Patternsrequire and Module Valuesrescue to Protected CallsRecap: Validate a BatchPractice on your own: Online Lua compiler