Created
October 15, 2025 15:58
-
-
Save havenwood/8a144ac7fd1a1d47658702286f056230 to your computer and use it in GitHub Desktop.
An example showing how to convert a Proc into a lambda with Fiddle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'fiddle' | |
| module ProcToLambda | |
| WORD = Fiddle::SIZEOF_VOIDP | |
| RPROC = 4 * WORD | |
| OFFSET = 32 | |
| VALUE = 2 | |
| refine Proc do | |
| def to_lambda | |
| return self if lambda? | |
| addr = Fiddle.dlwrap(self).to_i | |
| ptr = Fiddle::Pointer | |
| .new(addr + RPROC, WORD) | |
| .to_str | |
| .unpack1('Q') | |
| rproc = Fiddle::Pointer.new(ptr, OFFSET + WORD) | |
| rproc[OFFSET, WORD] = [VALUE].pack('Q') | |
| self | |
| end | |
| end | |
| end | |
| using ProcToLambda | |
| proc {}.to_lambda.lambda? | |
| #=> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment