lua-users home
lua-l archive

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


Hi!

I've have had some problems solving the error in the code below. I can't get gsub to accept a reference to a function local to the same object that the gsub-statement is enclosed in. self.replace returns a function naturally (print() proves that), but gsub fails anyway with the following error:

myhost% ./test.lua
This is my test text
error: attempt to index global `self' (a nil value)
stack traceback:
  1:  method `replaceAll' at line 13 [file `./test.lua']
  2:  function `main' at line 26 [file `./test.lua']
  3:  main of file `./test.lua' at line 30

self is nil? then how can I use self.Text in the replace() function?
I assume that there is some really simple solution or something that I've missed, but I can't find the reason for the error. Can you spot the error or at least point me to some literature that would explain it for me?

Thanks in advance!

/ Sebastian Rasmussen

function object(text)
 local instance = {}

 instance.Text = text

 function instance:print()
   print(self.Text)
 end

 function instance.replaceAll()
   self.Text = gsub(self.Text, " *([a-zA-Z]*) *", self.replace)
 end

 function instance:replace(word)
   reutrn "hello world"
 end

 return instance
end

function main()
 local o = object("This is my test text ")
 o:print()

 o:replaceAll()
 o:print()
end

main()


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.