Ancestors

Written by Simon George on 2024-05-17 at 15:07

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

Toot

Written by Simon George on 2024-05-17 at 15:08

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

Descendants

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/112457055307470667
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
249.725316 milliseconds
Gemini-to-HTML Time
0.412903 milliseconds

This content has been proxied by September (ba2dc).