lua-users home
lua-l archive

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


It was thus said that the Great Jonathan Goble once stated:
> On Sat, May 21, 2016 at 3:42 AM, Sean Conner <sean@conman.org> wrote:
> >>
> >   Hmm ... okay, how about:
> >
> > local version = ({Q="Lua 5.1",R="Lua 5.2",S="Lua 5.3"})[
> >         ("").dump(function() end):sub(5,5)] or "LuaJIT"
> >
> > And you can change it just as easily:
> >
> > local version = ({Q=true,R=false,S=false})[
> >         ("").dump(function() end):sub(5,5)] or true
> >
> >   -spc (Okay, so a few comments might be nice ... 8-)
> 
> After looking at the suggestions in this thread and experimenting with
> a few of them, I like the version by Egor in the OP the best. Why?
> Because unlike the other suggestions, it works without any environment
> at all.

[spc]lucy:/tmp>cat ex.lua
return ({Q="Lua 5.1",R="Lua 5.2",S="Lua 5.3"})[
        ("").dump(function() end):sub(5,5)] or "LuaJIT"

[spc]lucy:/tmp>lua-51
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> x = loadfile("ex.lua")
> setfenv(x,{})
> y = x()
> print(y)
Lua 5.1
> 
[spc]lucy:/tmp>lua-53 
Lua 5.3.2  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> x = loadfile("ex.lua","t",{})
> y = x()
> print(y)
Lua 5.3
> 

Unless luarocks removes the metatable from strings, my method still works.

  -spc (Will have to try this in a rockspec ... )