From: ara.t.howard@...
Date: 2006-04-12T01:16:36+09:00
Subject: Re: Oddities of Iteration:
On Wed, 12 Apr 2006, Nathan Olberding wrote:
> I'm working on a CGI script that iterates through a list of items of a
> class I've defined (class Report). Report::to_s just returns the string
> "yay!" for testing purposes.
>
> When I run this code:
> ---------------------
>
> puts "
Reports:"
> puts "
"
> reports.each do |x|
> begin
> puts "- " + x.to_s + "
"
> rescue
> puts "- ...
"
> end
> end
> puts "
"
> puts "
"
>
> ------------------------------
> This is the output (raw HTML):
> ------------------------------
>
> Incoming Reports:
>
> yay!
> - ...
> yay!
>
> - ...
> yay!
> - ...
> yay!
> - ...
> yay!
> - ...
> yay!
> - ...
> yay!
> - ...
>
> yay!
> - ...
> yay!
> - ...
> yay!
> - ...
>
>
>
>
> --------------------
>
> As you can see, the "yay!" is supposed to appear surrounded by li tags,
> but it never does.
>
> I have absolutely no idea why I'd need the exception handling code given
> my data set, but between each iteration, an exception is raised (can't
> convert nil in to String). It *is* saying "yay!" for each report in the
> data set, and I can verify this by making Report::to_s say something
> more identifying.
>
> So I'm trying to unravel two mysteries:
>
> 1) Why aren't the "yay!"s surrounded by li tags?
> 2) Why are exceptions being raised between each iteration, yet each item
> in the data set being iterated through passes through without exception?
>
> I realize this probably isn't enough code to discern all the
> peculiarities, but I'm just not sure what other parts would be useful.
> Sorry in advance!
probably you've defined 'to_s' as
class Report
def to_s
puts ...
end
end
instead of
class Report
def to_s
'some string'
end
end
which is outputing something, returning nil, and causing the error to be
thrown. this should be very easiy to debug: just take out your exception
handling code and run from the command line using
ruby ./index.cgi < /dev/null
you'll see the error instantly.
regards.
-a
--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama