On Feb 9, 2007, at 5:43 PM, Garance A Drosehn wrote:
> There's a situation that I occassionally run into, which I can work
> around easy enough, but it seems that there might be an cleaner
> solution than the ones I fall back on.  Let's say I want to have
> something like:
>
> ARGV.each { |arg|
>    case arg
>    when "-f"
>        ...do something with the *next* value of |arg|...
>    end
> }

Switch this to:

   list = ARGV.dup   # leave ARGV alone

   while item = list.shift
     case arg
     when "-f"
        file = list.shift
     end
   end

You can shift/unshift items as your logic dictates.  When there
is nothing left the loop ends.  You can break out of the loop
leaving items in ARGV via 'break'.

There are also several classes designed to process command line
arguments (e.g., GetoptLong from the standard library) that you could
use instead of rolling your own.


Gary Wright