From: Brian Candler Date: 2009-05-11T22:20:00+09:00 Subject: Re: CGI help Douglas Seifert wrote: > The interpreter sees that + at the end of the line in the first branch > of > the if statement and gets confused looking for something to add it to. > It > would be nice if it could figure out that you wanted to add it to the > result > of the statement that comes after the if statement, but that is not how > it > works. You would need to do something like like this to get it to work: > > (condition ? cgi.textarea() : cgi.textarea()) + > cgi.submit You can just use 'if' that way too, as it returns a value, although its low precedence means you'll need brackets: ( if condition foo else bar end ) + baz Personally I wouldn't try to write it this way; it's too error-prone as has been demonstrated already. I think it would be clearer to append to a target string in stages: str = "" if condition str << foo else str << baz end str << baz Here, each line stands by itself. But for any complex output it's probably better to forget about using CGI methods to generate HTML, and use some sort of template (e.g. ERB or HAML). Admittedly, in a one-shot CGI environment, there's a bigger startup overhead both in reading the template library and parsing the template, for every request. But if performance is a concern, then you shouldn't be using CGI anyway. If you write your app using a simple framework like Sinatra, then you can run it both as CGI and in a number of other environments, without changing the app. -- Posted via http://www.ruby-forum.com/.