Language LUA
(Examples of metatables in lua 5.1)
Date: | 06/07/07 |
Author: | Peter Cawley |
URL: | n/a |
Comments: | 0 |
Info: | http://www.lua.org/ |
Score: | (2.88 in 26 votes) |
phrase = "Take one down and pass it around," none = "No more bottles of beer" justone = "1 bottle of beer" bottles = setmetatable({0, drink = { phrase .. "\n" .. none .. " on the wall,", "Go to the store and buy some more!" }},{ __index = function(crate,num) crate[num] = nil print(crate[num] .. " on the wall,") print(crate[num] .. ",") print(phrase) crate[num] = crate[num-1] return justone .. " on the wall,\n" end, __newindex = function(crate,num,drink) rawset(crate,num,drink or (num .. " bottles of beer")) if rawget(crate,num+1) then print(crate[num] .. " on the wall,\n") end end, __call = function() return ipairs{justone, none} end} ) print(bottles[99]) for it, beer in bottles() do print(beer .. " on the wall,") print(beer .. ",") print(bottles.drink[it]) print "" end
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
original version | Philippe Lhoste | 04/20/05 | 1 | |
5.1, bottled | Peter Cawley | 04/19/09 | 0 | |
version 5.x | chill | 07/10/05 | 2 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments