This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
tball146's profile picture

metatable help

Started by tball146, 20 April 2016 - 01:20 AM
tball146 #1
Posted 20 April 2016 - 03:20 AM
i just wanna be corrected if im wrong

setmetatable(os,{__index=getfenv()})

this does mean the os table can explore the enviorment right?




P.S i cant spell
KingofGamesYami #2
Posted 20 April 2016 - 01:21 PM
No. It means os now contains everything in _G. For example, os.peripheral.wrap. Tables do not have environments.
Morganamilo #3
Posted 20 April 2016 - 04:26 PM
It means that if you try to index a value in os, if that index exists then it will return the value, otherwise it will try to find that value in getfenv() and return that.

You can think of it as


function lookup(k)
    if os[k] then
	    return os[k]
    else
	    return getfEnv()[k]
    end
end
tball146 #4
Posted 20 April 2016 - 05:14 PM
okay thank you
Bomb Bloke #5
Posted 20 April 2016 - 11:53 PM
No. It means os now contains everything in _G. For example, os.peripheral.wrap. Tables do not have environments.

Two problems; one, at least within ComputerCraft, you would not expect getfenv() to return _G; and two, the command does not actually put the elements from getfenv() into os - it merely makes them available through it. pairs() for eg won't see them in there.