Dir (objects introspection like Python's dir)


/ Published in: Lua
Save to your folder(s)



Copy this code and paste it in your HTML
  1. --- Returns string representation of object obj
  2. -- @return String representation of obj
  3. function dir(obj,level)
  4. local s,t = '', type(obj)
  5.  
  6. level = level or ' '
  7.  
  8. if (t=='nil') or (t=='boolean') or (t=='number') or (t=='string') then
  9. s = tostring(obj)
  10. if t=='string' then
  11. s = '"' .. s .. '"'
  12. end
  13. elseif t=='function' then s='function'
  14. elseif t=='userdata' then s='userdata'
  15. elseif t=='thread' then s='thread'
  16. elseif t=='table' then
  17. s = '{'
  18. for k,v in pairs(obj) do
  19. local k_str = tostring(k)
  20. if type(k)=='string' then
  21. k_str = '["' .. k_str .. '"]'
  22. end
  23. s = s .. k_str .. ' = ' .. dir(v,level .. level) .. ', '
  24. end
  25. s = string.sub(s, 1, -3)
  26. s = s .. '}'
  27. end
  28.  
  29. return s
  30. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.