I have a unix-inspired desire to pass arguments to a method where some
are toggle flags and some are values, aka “bash$ method --flag
–config=/etc/method”. I can think of a few ways to do it that are
close, but if I had a hash that initiated value-less keys to true, I
would have it.
method(:flag, :another, :config => “/etc/whatever”)
For some of the functionality I colud use *args and check
args.include?(:foo), but then I couldn’t pass in key-value pairs easily.
The closest way would be to use *args to pass in things of the form
method(:flag, [:config, “/etc/whatever”]), then use args.include?(:flag)
for flags and args.assoc(:config) for value-based options.
method(:flag, [:config, “/etc/whatever”])
if args.include?(:flag) do; stuff; end
if args.assoc(:config) do; @config = args.assoc(:config); end
I’d have to make the args.assoc logic prettier with a helper method to
set up default values, but that’s the basics of it. Much easier/prettier
if I could modify Hash to auto-assign empty keys to true. Note that
there’s a way to set the default value for empty keys from nil to
another value (and even a proc), but this wouldn’t be what I want. I
want empty keys to be nil, and specified lone keys set to true.
Thoughts? Anybody else think this would be really handy, or am I missing