lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The 'tostring' function calls a metamethod if one exists. The manual
is quite clear on that.

One might think that if the subject is already a string, the
__tostring metamethod is skipped, but that is not the case.

This feature could be exploited in many ways. Here is one.

getmetatable"".__tostring = function(s)
       local function tostr(a)
           if a<'!' or a>'~' then return a end
           if a==' ' then return '  ' end
           a = a:byte()+160
           if a<256 then return string.char(239,188,a-64) end
           return string.char(239,189,a-128)
           end
       return(s:gsub(".",tostr))
       end

print"Hello, moon, the eclipse is over!"