Code: Select all
function smt.__index(str, key)
if type(key) == 'number' then
return string.sub(str, key, key)
elseif stringMethods[key] then
return stringMethods[key]
end
end
Code: Select all
function smt.__index(str, key)
if type(key) == 'number' then
return string.sub(str, key, key)
elseif stringMethods[key] then
return stringMethods[key]
end
end
That's already possible...BlackBulletIV wrote:You might want to just make the __index function look at the string library, so can call all string functions from a string itself.
Code: Select all
local str = 'foo bar baz'
for i in str:match("%w+") do -- same as for i in string.match(str, "%w+")
print(i, i:sub(1,1))
end
print(("pi = %.5f"):format(math.pi)) -- shorthand for string.format("pi = %.5f", math.pi)
Code: Select all
function string.print(str, ...)
print(str, ...)
end
Code: Select all
string._ = string.format
print( ("pi = %.f5"):_(math.pi) )
If the key is a number, it'll get that character. If the key is a member of the stringMethods table, it'll return that.crow wrote:Hey I can see what the rest is doing but what is this one doing ?
Code: Select all
function smt.__index(str, key) if type(key) == 'number' then return string.sub(str, key, key) elseif stringMethods[key] then return stringMethods[key] end end
What could be nice:BlackBulletIV wrote:As for ('f'):print(), I couldn't be stuffed with that, it's actually more characters and I would assume it performs worse.
Code: Select all
function string:print()
print(self)
return self
end
do(something(with(strings:print()):print()):print()):print()
Users browsing this forum: Bing [Bot] and 3 guests