Ruby == to Shared __eq
Lesson 41 of 48 in Coddy's Lua for Ruby Developers course.
Ruby classes can override equality and Struct can compare members. Lua ordinary tables use identity, but Lua 5.1 can call __eq when the operands provide the same equality metamethod. Sharing a metatable is a straightforward way to ensure that requirement. Compare the intended value fields without conflating equality with identity; rawequal bypasses metamethod equality. Equal tables do not thereby become interchangeable value-based keys in another table.
Ruby
Point = Struct.new(:x, :y)
a, b = Point.new(1, 2), Point.new(1, 2)
puts a == b
puts a.equal?(b)Lua 5.1
local Point={__eq=function(a,b) return a.x==b.x and a.y==b.y end}
local a=setmetatable({x=1,y=2},Point)
local b=setmetatable({x=1,y=2},Point)
print(a==b)
Porting trap
Lua table keys use raw identity for table objects, even when __eq says two objects have equal coordinates.
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
EasyImplement point objects with Model.new(x,y) and Model.__eq. Input has x1,y1,x2,y2 as four integer lines. The app uses a == b and returns equal or different.
Edit app.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: Point equality must be symmetric, compare both x and y, and distinguish a change in either coordinate. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local Model=require("model")
local a,b,c=Model.new(1,2),Model.new(1,2),Model.new(1,3)
assert(a==b and b==a and a~=c and c~=a,"point equality compares both coordinates symmetrically")
local d=Model.new(2,2)
assert(a~=d,"x also participates in equality")
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