W
Woody Peterson
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.
Example:
methodflag, :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
methodflag, [:config, "/etc/whatever"]), then use args.include?flag)
for flags and args.assocconfig)[1] for value-based options.
Example:
methodflag, [:config, "/etc/whatever"])
ex. code:
if args.include?flag) do; stuff; end
if args.assocconfig) do; @config = args.assocconfig)[1]; 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
something?
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.
Example:
methodflag, :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
methodflag, [:config, "/etc/whatever"]), then use args.include?flag)
for flags and args.assocconfig)[1] for value-based options.
Example:
methodflag, [:config, "/etc/whatever"])
ex. code:
if args.include?flag) do; stuff; end
if args.assocconfig) do; @config = args.assocconfig)[1]; 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
something?