Created
September 3, 2025 20:15
-
-
Save havenwood/c57feb3b9909be7376cec81e638aa492 to your computer and use it in GitHub Desktop.
Set & get Ruby global variables using 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' | |
| require 'fiddle/import' | |
| module GlobalVariable | |
| extend Fiddle::Importer | |
| dlload Fiddle::Handle::DEFAULT | |
| extern 'void rb_gv_set(const char*, unsigned long)' | |
| extern 'unsigned long rb_gv_get(const char*)' | |
| end | |
| module Kernel | |
| def set_global_variable(name, value) = GlobalVariable.rb_gv_set(name.to_s, Fiddle.dlwrap(value)) | |
| def get_global_variable(name) = Fiddle.dlunwrap GlobalVariable.rb_gv_get name.to_s | |
| end | |
| set_global_variable(:$samp, 1_337) | |
| get_global_variable(:$samp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment