Oddities of Iteration

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 “<div style=“float:left;margin-right:2em”>Reports:”
puts “


    reports.each do |x|
    begin
    puts “
  1. ” + x.to_s + “

  2. rescue
    puts “

  3. end
    end
    puts “

puts “”

This is the output (raw HTML):

Incoming Reports:
    yay!
  1. ...
  2. yay!
  3. ...
  4. yay!
  5. ...
  6. yay!
  7. ...
  8. yay!
  9. ...
  10. yay!
  11. ...
  12. yay!
  13. ...
  14. yay!

  15. ...
  16. yay!
  17. ...
  18. yay!
  19. ...

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!

Nathan O. wrote:

begin
puts “

  • ” + x.to_s + “

  • rescue
    puts “

  • end
    end
    puts “”
    puts “”

    Just as a side note, you’ll probably find the CGI standard library
    easier to use than typing out the strings:

    http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html

    -Justin

    On Wed, 12 Apr 2006, Nathan O. wrote:

    begin

    yay!
    yay!
    but it never does.
    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

    Nathan O. wrote:

    In general, the CGI library will spit out exactly what you give it.
    There’s even a ‘pretty’ option so it will properly indent everything and
    make it easy to read, although I’ve found this can cause problems with
    textareas (extra whitespace).

    -Justin

    Justin C. wrote:

    Nathan O. wrote:

    Just as a side note, you’ll probably find the CGI standard library
    easier to use than typing out the strings:

    http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html

    Thanks! I’ve learned that having a one-line string for a method will
    return that string.

    CGI might be good under Ruby, but I’m definitely in the habit of writing
    my own HTML just because a lot of other languages’ modules generate some
    ugly HTML. I’ve got another project that’s just starting out. If Ruby’s
    CGI is prettier, I might go that way. I do know that I like its
    block-code styling.

    It does work:
    [~]% cat test.rb
    reports = [“yay!”] * 10

    puts “<div style=“float:left;margin-right:2em”>Reports:”
    puts “


      reports.each do |x|
      begin
      puts “
    1. ” + x.to_s + “

    2. rescue
      puts “

    3. end
      end
      puts “

    puts “”

    [~]% ruby test.rb

    Reports:
    1. yay!
    2. yay!
    3. yay!
    4. yay!
    5. yay!
    6. yay!
    7. yay!
    8. yay!
    9. yay!
    10. yay!

    My guess is that your to_s method does
    puts “yay!”
    and raises an exception, instead of just returning yay! (def to_s;
    “yay!”
    end)
    Could you show us the definition of to_s ?
    Regards,
    Sylvain

    On 4/11/06, Nathan O. [email protected] wrote:

    puts “”

    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?

    My guess, your method is defined like this:

    class Report
    def to_s
    puts “yay!”
    end
    end

    As a result, in each iteration, x.to_s prints “yay!” on it’s own line.
    Then x.to_s returns nil (the return value of the puts statement) and

  • ” + nil tries to coerce nil into a String but fails, raising the
    exception.

    Try redefining your method as such:

    class Report
    def to_s
    “yay!”
    end
    end

    Jacob F.

  • Hi all-
    I need to automate a few function in an Access form. The form will
    already be opened, (basically I need to push a button and fill out a few
    things in the window that opens, then close the newly opened form and
    click OK on the dialog box that pops up). So first, basically I need to
    attach to the opened Access form, an then if anyone has any clues on
    finding ole commands to do that other things that would be great.

    Thanks,
    Dave

    Jacob F. wrote:

    Try redefining your method as such:

    class Report
    def to_s
    “yay!”
    end
    end

    You’re both right, like I (cryptically) said before, I had the “puts” in
    there. Took it out, things work great. Thanks!

    To add to James’ good suggestions, I’d suggest finding an OLE browser.
    I know VB6 has one, and so should other M$ dev tools if you have
    access to them.

    If you don’t have access to those, ActiveState has a spiffy little OLE
    browser included with it’s free Perl binaries.

    hth,

    Dave K. wrote:

    Hi all-
    I need to automate a few function in an Access form. The form will
    already be opened, (basically I need to push a button and fill out a few
    things in the window that opens, then close the newly opened form and
    click OK on the dialog box that pops up). So first, basically I need to
    attach to the opened Access form, an then if anyone has any clues on
    finding ole commands to do that other things that would be great.

    Look at AutoItX (free software to script mouse clicks and keyboard
    entries). It’s scriptable with Ruby + Win32OLE


    James B.

    “The greatest obstacle to discovery is not ignorance, but the illusion
    of knowledge.”

    • D. Boorstin