Forum: Ruby Oddities of Iteration

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nathan O. (Guest)
on 2006-04-11 20:06
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!
unknown (Guest)
on 2006-04-11 20:18
(Received via mailing list)
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
Justin C. (Guest)
on 2006-04-11 20:27
(Received via mailing list)
Nathan O. wrote:
> 	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
Nathan O. (Guest)
on 2006-04-11 20:34
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.
Justin C. (Guest)
on 2006-04-11 20:42
(Received via mailing list)
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
Sylvain J. (Guest)
on 2006-04-11 20:57
(Received via mailing list)
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
Jacob F. (Guest)
on 2006-04-11 21:31
(Received via mailing list)
On 4/11/06, Nathan O. <removed_email_address@domain.invalid> wrote:
> 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 F.
Nathan O. (Guest)
on 2006-04-11 21:40
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!
Dave K. (Guest)
on 2006-04-11 21:59
(Received via mailing list)
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
James B. (Guest)
on 2006-04-11 22:18
(Received via mailing list)
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
Keith S. (Guest)
on 2006-04-11 22:37
(Received via mailing list)
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,
This topic is locked and can not be replied to.