Jump to content




FileMagic - demonstrating the power of metatables


  • You cannot reply to this topic
7 replies to this topic

#1 Jan

  • Members
  • 141 posts
  • Locationthe Netherlands

Posted 27 October 2012 - 03:14 PM

So I was testing with metatables, and then I came up with this.
Instead of messing with io.open, you can just open a file by indexing a table!
for example:
print(files.test)
would print the contents of the file 'test'
files.test = "Some text"
Would write "some text" in the file 'test'

Just put this in front of your program:
local mt = {}
local function getMagicHandle()
local t = {}
setmetatable(t,mt)
return t
end
mt.__index = function (t,i)
 local handle = io.open(i,"r")
 if handle then
  local s =  handle:read("*a")
  handle:close()
  return s
 end
end
mt.__newindex = function(t,i,v)
 if type(v)=="nil" then
  fs.delete(i)
 elseif type(v)=="string" then
  local handle = io.open(i,"w")
  if handle then
   handle:write(v)
   handle:close()
  end
 end
end 
files = getMagicHandle()

You can also run the program once, and try it out in the 'lua' program.

EDIT:
You can now delete a file by setting it to nil

#2 ChunLing

  • Members
  • 2,027 posts

Posted 28 October 2012 - 01:08 AM

Nice.

#3 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 28 October 2012 - 01:14 AM

nice program

i have a system with advanced metatables and can store variables on a remote server in real time
you can even edit tables within tables :D/>

#4 Jazza_Hat

  • New Members
  • 6 posts

Posted 29 October 2012 - 08:12 AM

Theoretically if you serialized a table you could save tables too?

#5 Jan

  • Members
  • 141 posts
  • Locationthe Netherlands

Posted 30 October 2012 - 09:26 PM

View PostJazza_Hat, on 29 October 2012 - 08:12 AM, said:

Theoretically if you serialized a table you could save tables too?
Yup
files.somefile = textutils.serialize(sometable)

View PostPixelToast, on 28 October 2012 - 01:14 AM, said:

nice program

i have a system with advanced metatables and can store variables on a remote server in real time
you can even edit tables within tables :P/>
Thanks :P/>
Yes, changing a table on another computer could be very handy!

#6 gknova61

  • Members
  • 74 posts

Posted 09 November 2012 - 03:04 PM

Does this make them persistent across restarts?

#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 09 November 2012 - 04:04 PM

View Postgknova61, on 09 November 2012 - 03:04 PM, said:

Does this make them persistent across restarts?

Files are persistent, variables are not.

#8 Sebra

  • Members
  • 726 posts

Posted 19 November 2012 - 04:17 AM

NIce, but...
As you unable to name a variable with "/" you tied to single directory.
And that give out a suggestion to change your code for this:
dir=magicHandler("some//dir//needed")
dir.filename = "Some text to store."
... or this:
dir.subdir.file = "content"
Both variants are interesting, second is more challenging.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users