Recap: Dispatch Reports
Lesson 42 of 48 in Coddy's Lua for Ruby Developers course.
Challenge
MediumImplement two modules, short.lua and full.lua, each providing format(self,name,n). Short returns name:n; full returns name has n. Input has name, integer n, and style short or full. App selects the module and returns its format result.
Edit app.lua, short.lua, full.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: Both formatter modules must implement their public format(self, name, n) method independently. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local short,full=require("short"),require("full")
assert(short:format("pen",3)=="pen:3","short module format")
assert(full:format("book",0)=="book has 0","full module format")
assert(short:format("x",-1)=="x:-1","signed values are formatted unchanged")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
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 Inventory8Composition and Object Lookup
to_s to __tostringComposition Through TablesInheritance to Lookup ChainsDuck-Typed Message ContractsRuby == to Shared __eqRecap: Dispatch ReportsPractice on your own: Online Lua compiler