On Fri, 9 Nov 2001, Dave Thomas wrote:

>   err(...) unless ARGV[0] && !ARGV[0].empty?

> There's no need to test for defined?, as the array will return nil for
> missing elements.
>
That would work, but at the point I am testing this it is not coming
from ARGV. I am using getoptlong and setting a member variable
for each param. If the flag is not present, then the variable doesn't
get defined. I suppose I could set the params to nil by default.


> It doesn't catch the case when I run it with empty strings, but that's
> not something I tend to do too often :)

Since these are parameters with arguments coming from getoptlong,
I can't think of how they would be empty. So, maybe I could do:

class App
  def initialize
    @param1 = nil
    @param2 = nil
    get_opts
  end

  def get_opts
      opts = GetoptLong.new(
        [ "--param1", "-p", GetoptLong::NO_ARGUMENT ],
        [ "--param2", "-s", GetoptLong::REQUIRED_ARGUMENT ])

      opts.each do |opt, arg|
        case opt
          when "--param1"
            @param1 = true
          when "--param2"
            @param1 = arg
        end
     end
     ## we know that paramx is defined and possbiley nil
     ## also, paramx cannot be empty since getoptlong
     ## cannot assign '' to arg.
     if @param1.nil?
       STDERR.puts "error message here"
       usage
       exit 1
     end
     if @param2.empty?
       STDERR.puts "error message here"
       usage
       exit 1
     end

  end#get_opts
end#class App

App.new.run



=========================================================
Jim Freeze
jim / freeze.org
---------------------------------------------------------
Today is a fine day for Ruby programming.
http://www.freeze.org
=========================================================