On Fri, 9 Nov 2001, Jim Freeze wrote:

>   if !defined?(param) || param.nil? || param.empty?
>     # err
>   end

One way to simplify this is to use my call stack hack from
[ruby-talk:22963], then do the following:

  def is_empty(param)
    return eval(
        "!defined?(#{param}) || #{param}.nil? || #{param}.empty?",
        $call_stack[0][4])
  end

  p is_empty("foo")   #=> true
  foo = Hash.new
  p is_empty("foo")   #=> true
  foo[0] = 1
  p is_empty("foo")   #=> false

the disadvantage here is that it is slow (I've also got a C implementation
of the hack, which is faster, but still has a speed hit).

Another option, which is not as clean from the user's point of view, is:

  class NilClass
    def empty?
      true
    end
  end

  if (param ||= nil).empty?
    # err
  end

Of course, this has the side effect of creating param if it does not
exist.

AFAIK, there's no good way in Ruby to do what you want.  Perhaps there is
a better way to do what you want without having to check to see if a
certain variable has been defined?  Often (but certainly not always), if
it's not possible to do something in Ruby, there is a good reason.

Paul