"Hugh Sasse Staff Elec Eng" <hgs / dmu.ac.uk> schrieb im Newsbeitrag
news:Pine.GSO.4.53.0309101847560.23363 / neelix...
> Reading about reflection, ObjectSpace will give you the objects in
> your system, possibly selected by Class.  Fine.  It won't tell you
> which variables are associated with them.  You can trace on global
> variables, you can look at instance variables, but how can you get
> information about all variables?  What *is* a variable, exactly, come
> to that?  It works like a sticky label so you can have two on the
> same object, but are the variables themselve objects in ruby?  Can I
> twist ObjectSpace's arm to tell me about them?
>
> Why have I run into this now?
>
> I'm getting
> #<TypeError: failed to convert nil into Array>
> raised by a program where
>     rescue => e
>       p e, caller
> gives some line numbers with an each keyword, where the code looks like
>     raise "@b1 is nil" if @b1.nil
>     @b1.each { |bf|
> so I know that @b1 is not nil when it gets here.  The each method is
> one I wrote, so I'd expect it to blow up in there, with an
> associated line number.

There are two issues with the line "raise "@b1 is nil" if @b1.nil?":

1. The test is inappropriate, what you really want is to ensure that the
obj referred to by @b has method "each".  So these are better alternatives
(but see issue 2):

raise "Wrong @b1" unless @b1.kind_of? Enumerable
raise "Wrong @b1" unless @b1.respond_to? :each

2. Btw, generally it's superfluous to include the explicite test and raise
since if the instance referred to by @b does not have an "each" method the
code will blow anyway with an NoMethodError.  So omitting this test is
shorter and cleaner.

> Can I get ruby to tell me all things that point at the one Nil
> object in the universe, at the present time?

Well, you could do this

unless @b1.respond_to? :each
  p @b1 # prints info about @b1
  puts @b1.class # print type
  # print other informative stuff

  raise "No each"
end

or use the debugger.

But as I said before, generally this is not a too good idea.

Regards

    robert