Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created September 3, 2025 20:15
Show Gist options
  • Save havenwood/c57feb3b9909be7376cec81e638aa492 to your computer and use it in GitHub Desktop.
Save havenwood/c57feb3b9909be7376cec81e638aa492 to your computer and use it in GitHub Desktop.
Set & get Ruby global variables using Fiddle
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