0

How can you replace all the functions for a particular library in package.loaded after a require call?

I've tried to iterate over the relevant table but the table comes up empty.

local aLibrary = require "aLibrary"

for key,value in ipairs(package.loaded.aLibrary) do
    package.loaded.aLibrary[key] = function() end
end
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Puddler
  • 2,619
  • 2
  • 17
  • 26
  • 1
    Why do you want or need this? – lhf Jul 29 '15 at 13:04
  • @lhf: `aLibrary` is used by other modules, and it assumes that certain resources are available. I need to replace the function calls to `aLibrary` so I can use the other modules even though the resources aren't available. – Puddler Jul 30 '15 at 01:55

2 Answers2

3

The simpler code below should do it (but note the use of pairs instead of ipairs).

local aLibrary = require "aLibrary"

for key in pairs(aLibrary) do
    aLibrary[key] = function() end
end

Note that require does not return a copy of the library table and so the code above affects its contents without replacing the library table.

In other words, any later call to require "aLibrary" will return the table with the new functions. If you don't want that to happen, then you probably need a new table instead of changing its contents.

lhf
  • 70,581
  • 9
  • 108
  • 149
2

How about using pairs to go through keys instead of only indices?

Youka
  • 2,646
  • 21
  • 33