Inheritance to Lookup Chains
Lesson 39 of 48 in Coddy's Lua for Ruby Developers course.
Ruby class inheritance provides automatic method lookup and super. Lua object patterns construct lookup chains explicitly through __index. An instance can fall back to a child method table, which in turn falls back to a base table. Missing lookup does not copy fields into the instance. To call overridden base behavior, name the base function and pass the current receiver explicitly. Lua has no built-in Ruby super keyword.
Ruby
class Base
def value; 3; end
end
class Child < Base
end
puts Child.new.valueLua 5.1
local Base={}
function Base:value() return 3 end
local Child=setmetatable({}, {__index=Base})
Child.__index=Child
local c=setmetatable({},Child)
print(c:value())
Porting trap
Do not assume all metamethods are inherited through an ordinary __index chain. Special-operation lookup has its own metatable rules.
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
EasyBase stores a unit price and has total(quantity). Model is a child prototype overriding total to add a flat fee exactly once, including zero quantity. Input is price, quantity, fee. Return price*quantity+fee. Keep Base in base.lua.
Edit app.lua, base.lua, model.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: Base.total(receiver, quantity) multiplies receiver.price by quantity. Model.new(price, fee) returns independent objects whose total adds the fee once, even for zero quantity. Only observable API behavior is checked. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local Base=require("base")
local Model=require("model")
assert(Base.total({price=4},3)==12,"base total multiplies receiver price by quantity")
local a,b=Model.new(3,2),Model.new(5,1)
assert(a:total(4)==14 and a:total(0)==2,"child total adds the flat fee once")
assert(b:total(2)==11 and a:total(1)==5,"instances retain independent price and fee")
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 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