Created
October 8, 2025 15:15
-
-
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)
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
| 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