lua-users home
lua-l archive

Privately patching strings lib in Lua 5.2

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


```lua
_ENV = {
 print = print, string = string, table = table, pairs = pairs, type = type
}
do
 local _string = string
 local concat = table.concat
 string = {}
 for m_name, m_func in pairs(_string) do
 string[m_name] = m_func
 end
 function string:map(...)
 local args = type(...) == "table" and (...) or {...}
 return self .. concat(args, self)
 end
end
--testing
--This is result that I want.
print(string.map("test","this", "that"))
-->testthistestthat
--This is the syntax that I want to use, but it doesn't work...
print(("test"):map("this", "that"))
--lua: ...src\testing\test_string_library_question.lua:23: attempt to
call method 'map' (a nil value)
```
If I skip the parts where I hide my change from the global
environment, then both function calls behave identically.
Is there a way to patch the `string` library so that the changes apply
to strings using the `self`/colon method, without patching `string`
globally? Debug library?
--Andrew

AltStyle によって変換されたページ (->オリジナル) /