From: Eric Schwartz Date: 2004-02-20T20:23:03+09:00 Subject: Re: another silly cgi question Cere Davis writes: > Hello Ruby cgi geniuses, > > I cannot figure out how to print html content within the same scope as > the cgi.xx object. That is: When I try and run this: > > > cgi.out() do > cgi.html() do > cgi.body() do > p userinfo.to_s ^^^^^^^^^^^^^^^ That's your problem. I'm not sure what 'userinfo' is here, but the pickaxe says about Kernel#p: For each object, directly writes anObject.inspect followed by the current output record separator to the program's standard output. p bypasses the Ruby I/O libraries That means that as soon as you call 'p userinfo.to_s', that gets printed immediately, instead of as part of your CGI output. This Is Bad(tm). The block you pass to CGI#body should return a string, which will be the contents of the '' '' tags. Instead, you're printing something to stdout, then building your string below, and after the string is built, the cgi routines spit out their HTML elements, with your string inside it. Also, your indentation is inconsistent and too hard to read at the start for my taste, and you don't need the () if you're not passing parameters to the HTML-generating methods. I'd prefer something that looked like: cgi.out { # or 'do'..'end', whatever you like cgi.html { cgi.body { userinfo.to_s + cgi.form('get') { # I only use "" when I'm interpolating variables cgi.params.collect { |key,val| key + ' ' + # HTML collapses spaces, so no need to put cgi.text_field(key,val) + # multiple ones here. cgi.br }.join("\n") + # I like "\n" here, as it makes the generated cgi.br + # HTML easier to read. cgi.submit("How's this?") } } } } > Does anyone know how to shim your own html into the cgi object so > that it will run correctly? I realize that at this very instant, Ruby makes that harder than necessary, but "Programming Ruby" is a decent reference, and is (most importantly) free. If you check out the documentation there, you'll see they provide a better and more complete explanation of cgi.rb than I have here. Thanks to Dave Thomas and many others, 'ri' will soon become the standard reference for Ruby documentation. I can't wait for the day the default response to any 'what is wrong with this code?' query becomes, "Did you read the documentation on your hard drive?" -=Eric -- Come to think of it, there are already a million monkeys on a million typewriters, and Usenet is NOTHING like Shakespeare. -- Blair Houghton.