Ruby challenge! Can anyone find a way to get the names of the local variables defined within an arbitrary lambda without changing the lambda? Example lambda:
prk = ->{a=1, b=2} [#] … some_magic(prk) #=> [:a, :b]
My first thought was prk.binding.local_variables
but Proc#binding
returns the binding of the caller not the inner binding. And it's an arbitrary lambda so no variable shadowing ;-)
It's for a silly personal project not prod so I'm happy with weird metaprogramming!
=> More informations about this toot | More toots from sfcgeorge@ruby.social
I did find one way using RubyVM but it doesn't work in ruby.wasm so I can't use it:
RubyVM::AbstractSyntaxTree.of(prk).children.first.zip(prk.call).to_h [#] => { :a => 1, :b => 2 }
=> More informations about this toot | More toots from sfcgeorge@ruby.social
text/gemini
This content has been proxied by September (ba2dc).