Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created October 8, 2025 15:15
Show Gist options
  • Save havenwood/bc8894aa0bb28705d941b0c073673d68 to your computer and use it in GitHub Desktop.
Save havenwood/bc8894aa0bb28705d941b0c073673d68 to your computer and use it in GitHub Desktop.
An example of `Ruby::Box` isolating globals (due to be released on Christmas)
class Options < Ruby::Box
def [](name) = instance_eval { eval "$#{name.to_s.delete_prefix('$')}" }
end
options = Options.new
options.instance_eval do
require 'optparse'
class Option
def []=(key, value)
key = key.to_s.tr('-', '_')
raise ArgumentError, "Bad key: #{key}" unless key.match?(/\A\w+\z/)
eval "$#{key} = value"
end
end
OptionParser.new { it.on '--warn' }.parse!(into: Option.new)
end
warn 'oops' if options[:warn]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment