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 "<ol>"
reports.each do |x|
  begin
    puts "<li>" + x.to_s + "</li>"
  rescue
    puts "<li>...</li>"
  end
end
puts "</ol>"
puts "</div>"

------------------------------
This is the output (raw HTML):
------------------------------

<div style="float:left;margin-right:2em">Incoming Reports:
<ol>
yay!
<li>...</li>
yay!

<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>

yay!
<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>
</ol>
</div>

--------------------

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!

--
Posted via http://www.ruby-forum.com/.

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

···

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 "<div style=\"float:left;margin-right:2em\">Reports:"
puts "<ol>"
reports.each do |x|
  begin
    puts "<li>" + x.to_s + "</li>"
  rescue
    puts "<li>...</li>"
  end
end
puts "</ol>"
puts "</div>"

------------------------------
This is the output (raw HTML):
------------------------------

<div style="float:left;margin-right:2em">Incoming Reports:
<ol>
yay!
<li>...</li>
yay!

<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>

yay!
<li>...</li>
yay!
<li>...</li>
yay!
<li>...</li>
</ol>
</div>

--------------------

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!

--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama

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 "<div style=\"float:left;margin-right:2em\">Reports:"
puts "<ol>"
reports.each do |x|
  begin
    puts "<li>" + x.to_s + "</li>"
  rescue
    puts "<li>...</li>"
  end
end
puts "</ol>"
puts "</div>"
  
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

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

puts "<div style=\"float:left;margin-right:2em\">Reports:"
puts "<ol>"
reports.each do |x|
    begin
        puts "<li>" + x.to_s + "</li>"
    rescue
        puts "<li>...</li>"
    end
end
puts "</ol>"
puts "</div>"

[~]% ruby test.rb
<div style="float:left;margin-right:2em">Reports:
<ol>
<li>yay!</li>
<li>yay!</li>
<li>yay!</li>
<li>yay!</li>
<li>yay!</li>
<li>yay!</li>
<li>yay!</li>
<li>yay!</li>
<li>yay!</li>
<li>yay!</li>
</ol>
</div>

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

puts "<div style=\"float:left;margin-right:2em\">Reports:"
puts "<ol>"
reports.each do |x|
        begin
                puts "<li>" + x.to_s + "</li>"
        rescue
                puts "<li>...</li>"
        end
end
puts "</ol>"
puts "</div>"

<snip>

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
"<li>" + 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 Fugal

···

On 4/11/06, Nathan Olberding <nathan.olberding@gmail.com> wrote:

Justin Collins wrote:

Nathan Olberding 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.

···

--
Posted via http://www.ruby-forum.com/\.

Jacob Fugal 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!

···

--
Posted via http://www.ruby-forum.com/\.

Nathan Olberding wrote:

Justin Collins wrote:
  

Nathan Olberding 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.

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

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

Dave King 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 Britt

"The greatest obstacle to discovery is not ignorance, but the illusion of knowledge."
  - D. Boorstin

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,

···

On 4/11/06, James Britt <james_b@neurogami.com> wrote:

Dave King 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 Britt

"The greatest obstacle to discovery is not ignorance, but the illusion
of knowledge."
  - D. Boorstin

--
Keith Sader
ksader@gmail.com
http://www.saderfamily.org/roller/page/ksader