# CGI and Methods?

**URL:** <https://rubytalk.org/t/cgi-and-methods/45247>\
**Category:** ruby-talk\
**Created:** [17 March 2008 23:27 UTC](https://rubytalk.org/t/cgi-and-methods/45247 "2008-03-17T23:27:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_De1](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tony\_De1](https://rubytalk.org/u/Tony_De1)\
**Post date:** [17 March 2008 23:27 UTC](https://rubytalk.org/t/cgi-and-methods/45247/1 "2008-03-17T23:27:19Z")

</div>

Hi all,

I'm trying my hand at scripting cgi in ruby. At the same time I'm  
looking at Rails. So far I love ruby. Still reserving comments on  
Rails. I have a question though. When coding a cgi script, is it  
possible to have methods as I would say in a n my\_app.rb or rails app?  
Below is a script I'm working on to provide a web interface to our  
syslog servers logs for our tech support dept. I would like to DRY this  
up a bit. I'm repeating entirely too much code. I would welcome not  
only a response to my question, but any other suggestions/criticism to  
aid me in my learning process. Thank you very much.

tonyd

#!/usr/local/bin/ruby -w

> **···**
>
> #  
> # Filename: syssearch.cgi  
> #  
> # Description:  
> # Open log(s) file and do our searching before we begin to send  
> # data back to the browser. This way we don't cause the browser to time  
> out  
> # (in theory...)  
> #  
> # Inputs: none
> 
> require 'cgi'
> 
> cgi = CGI.new
> 
> # Our search params passed  
> search\_string = cgi['searchit']  
> slogtype = cgi['logtype']  
> smethod = cgi['method']
> 
> # Do we have something to search on?  
> if search\_string.empty?  
> &nbsp;&nbsp;puts "Content-type: text/plain\r\n\r\n\<HTML\>\n\<HEAD\>\n\<TITLE\>CommSpeed  
> Log Search\</TITLE\>\n\</HEAD\>\n\<BODY\>\n"  
> &nbsp;&nbsp;puts "\<h1\>Please enter a search criteria\</h1\>"  
> &nbsp;&nbsp;puts "\</BODY\>\n\</HTML\>\n"  
> &nbsp;&nbsp;exit  
> end
> 
> # A hash of possible logs  
> logs = {'smtp' =\> 'smtp', 'mail' =\> 'maillog', 'mx' =\> 'mx', 'spam' =\>  
> 'spam', 'Dial Up' =\> 'radius', 'DHCP' =\> 'dhcp' }
> 
> if smethod == "CAT"  
> &nbsp;&nbsp;# Array to hold our search result matches  
> &nbsp;&nbsp;results = Array.new
> 
> &nbsp;&nbsp;# Open file and look for our search string  
> &nbsp;&nbsp;File.open("/var/log/"+logs[slogtype],  
> 'r').grep(/#{search\_string}/).each do |line|  
> &nbsp;&nbsp;results.push(line) # append it to our results array  
> end # file is automatically closed
> 
> &nbsp;&nbsp;if results.empty?  
> &nbsp;&nbsp;&nbsp;&nbsp;# No matches found  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Content-type:  
> text/plain\r\n\r\n\<HTML\>\n\<HEAD\>\n\<TITLE\>CommSpeed Log  
> Search\</TITLE\>\n\</HEAD\>\n\<BODY\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\<h1\>No DHCP Log Results Found!\</h1\>"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\</BODY\>\n\</HTML\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;# Now send results back to browser  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Content-type:  
> text/plain\r\n\r\n\<HTML\>\n\<HEAD\>\n\<TITLE\>CommSpeed Log  
> Search\</TITLE\>\n\</HEAD\>\n\<BODY\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\<h1\>DHCP Log Results...\</h1\>"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Print the results  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results.each do |line|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts line+"\<br\>"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# Close HTML  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\</BODY\>\n\</HTML\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
> &nbsp;&nbsp;end  
> end
> 
> if smethod == "TAIL"  
> &nbsp;&nbsp;#TODO  
> &nbsp;&nbsp;#Need to figure out how to tail a file with our search scope in place  
> and feed it to the browser window.  
> end  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [18 March 2008 02:14 UTC](https://rubytalk.org/t/cgi-and-methods/45247/2 "2008-03-18T02:14:55Z")

</div>

Tony De wrote:

> Hi all,
> 
> I'm trying my hand at scripting cgi in ruby. At the same time I'm  
> looking at Rails. So far I love ruby. Still reserving comments on  
> Rails. I have a question though. When coding a cgi script, is it  
> possible to have methods as I would say in a n my\_app.rb or rails app?  
> Below is a script I'm working on to provide a web interface to our  
> syslog servers logs for our tech support dept. I would like to DRY this  
> up a bit. I'm repeating entirely too much code. I would welcome not  
> only a response to my question, but any other suggestions/criticism to  
> aid me in my learning process. Thank you very much.
> 
> tonyd  
> &nbsp;&nbsp;  
> Writing code using the CGI library is just like any other Ruby code, so you can have any methods, classes, or other code that you want.

> &nbsp;&nbsp;&nbsp;&nbsp;puts "Content-type:  
> text/plain\r\n\r\n\<HTML\>\n\<HEAD\>\n\<TITLE\>CommSpeed Log  
> Search\</TITLE\>\n\</HEAD\>\n\<BODY\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\<h1\>No DHCP Log Results Found!\</h1\>"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\</BODY\>\n\</HTML\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit

The CGI library does this kind of HTML work for you. See the example in about the middle of the page here:  
[http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html](http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html)  
and the documentation here:  
[http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/HtmlExtension.html](http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/HtmlExtension.html)

-Justin

---

<div class="post-metadata">

**Author:** ![Tony\_De1](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tony\_De1](https://rubytalk.org/u/Tony_De1)\
**Post date:** [18 March 2008 03:58 UTC](https://rubytalk.org/t/cgi-and-methods/45247/3 "2008-03-18T03:58:41Z")

</div>

Justin Collins wrote:

> Tony De wrote:
> 
> > aid me in my learning process. Thank you very much.
> > 
> > tonyd  
> > &nbsp;&nbsp;  
> > Writing code using the CGI library is just like any other Ruby code, so  
> > you can have any methods, classes, or other code that you want.
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Content-type:  
> > text/plain\r\n\r\n\<HTML\>\n\<HEAD\>\n\<TITLE\>CommSpeed Log  
> > Search\</TITLE\>\n\</HEAD\>\n\<BODY\>\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "\<h1\>No DHCP Log Results Found!\</h1\>"  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "\</BODY\>\n\</HTML\>\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit
> 
> The CGI library does this kind of HTML work for you. See the example in  
> about the middle of the page here:  
> [http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html](http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html)  
> and the documentation here:  
> [http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/HtmlExtension.html](http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/HtmlExtension.html)
> 
> -Justin

Thanks Justin,

That's what I thought. However, when I place a:

def my\_method  
end

statement, my code dies. Even if I simply make the definition with no  
additional code. Do I need to place it at the top, bottom? And does it  
have something to do with the extension I place on the file, .cgi vs  
.rb?

tonyd

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [18 March 2008 08:22 UTC](https://rubytalk.org/t/cgi-and-methods/45247/4 "2008-03-18T08:22:13Z")

</div>

Tony De wrote:

> Justin Collins wrote:  
> &nbsp;&nbsp;
> 
> > Tony De wrote:  
> > &nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > > aid me in my learning process. Thank you very much.
> > > 
> > > tonyd  
> > > &nbsp;&nbsp;
> > 
> > Writing code using the CGI library is just like any other Ruby code, so  
> > you can have any methods, classes, or other code that you want.
> > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;puts "Content-type:  
> > > text/plain\r\n\r\n\<HTML\>\n\<HEAD\>\n\<TITLE\>CommSpeed Log  
> > > Search\</TITLE\>\n\</HEAD\>\n\<BODY\>\n"  
> > > &nbsp;&nbsp;&nbsp;&nbsp;puts "\<h1\>No DHCP Log Results Found!\</h1\>"  
> > > &nbsp;&nbsp;&nbsp;&nbsp;puts "\</BODY\>\n\</HTML\>\n"  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > The CGI library does this kind of HTML work for you. See the example in  
> > about the middle of the page here:  
> > [http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html](http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html)  
> > and the documentation here:  
> > [http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/HtmlExtension.html](http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/HtmlExtension.html)
> > 
> > -Justin  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > Thanks Justin,
> 
> That's what I thought. However, when I place a:
> 
> def my\_method  
> end
> 
> statement, my code dies. Even if I simply make the definition with no additional code. Do I need to place it at the top, bottom? And does it have something to do with the extension I place on the file, .cgi vs .rb?
> 
> tonyd  
> &nbsp;&nbsp;  
> It does matter where you place the method definition, in a way. That is, you can only use it in the code following its definition.

For example:

&nbsp;&nbsp;def my\_method  
&nbsp;&nbsp;end

&nbsp;&nbsp;my\_method

will work, but

&nbsp;&nbsp;my\_method

&nbsp;&nbsp;def my\_method  
&nbsp;&nbsp;end

will not.

Are you getting a specific error message?  
If your code is working with .cgi as it is, adding methods should not change a thing.

-Justin

---

<div class="post-metadata">

**Author:** ![Tony\_De1](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tony\_De1](https://rubytalk.org/u/Tony_De1)\
**Post date:** [18 March 2008 09:01 UTC](https://rubytalk.org/t/cgi-and-methods/45247/5 "2008-03-18T09:01:11Z")

</div>

Justin Collins wrote:

> Tony De wrote:
> 
> > > you can have any methods, classes, or other code that you want.  
> > > about the middle of the page here:
> > 
> > tonyd  
> > &nbsp;&nbsp;  
> > It does matter where you place the method definition, in a way. That is,  
> > you can only use it in the code following its definition.
> 
> For example:
> 
> &nbsp;&nbsp;def my\_method  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;my\_method
> 
> will work, but
> 
> &nbsp;&nbsp;my\_method
> 
> &nbsp;&nbsp;def my\_method  
> &nbsp;&nbsp;end
> 
> will not.
> 
> Are you getting a specific error message?  
> If your code is working with .cgi as it is, adding methods should not  
> change a thing.
> 
> -Justin

Justin,

I don't recall the exact error message now, it was late am yesterday.  
But that's exactly what I was doing. I was defining my method at the  
bottom of my code file, much as I would in a rails controller, sort of.

So that I am clear on the file extension stuff. And for your info to  
properly respond to my question; our servers run FreeBSD, Apache2, and  
on this testing platform, mod\_ruby. ? Do I need to use .cgi or .rb?  
Does it matter where the file is placed? Seemingly stupid questions,  
(and new to ruby) we know that a .cgi file is typically placed in the  
user\_web\_space cgi dir (or based on our tailored Apache config) to be  
executed. Would the same .rb file placed in the cgi dir be executed if  
called from the html? Would it need to be placed elsewhere? And/or are  
we talking about an Apache config mod? Was that clear as mud? Thanks  
greatly for your assistance. You've already been a great help.

tonyd

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [18 March 2008 16:37 UTC](https://rubytalk.org/t/cgi-and-methods/45247/6 "2008-03-18T16:37:45Z")

</div>

Tony De wrote:

> Justin Collins wrote:  
> &nbsp;&nbsp;
> 
> > Tony De wrote:  
> > &nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > > > you can have any methods, classes, or other code that you want.  
> > > > about the middle of the page here:  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > > 
> > > tonyd  
> > > &nbsp;&nbsp;
> > 
> > It does matter where you place the method definition, in a way. That is,  
> > you can only use it in the code following its definition.
> > 
> > For example:
> > 
> > &nbsp;&nbsp;def my\_method  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;my\_method
> > 
> > will work, but
> > 
> > &nbsp;&nbsp;my\_method
> > 
> > &nbsp;&nbsp;def my\_method  
> > &nbsp;&nbsp;end
> > 
> > will not.
> > 
> > Are you getting a specific error message?  
> > If your code is working with .cgi as it is, adding methods should not  
> > change a thing.
> > 
> > -Justin  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > Justin,
> 
> I don't recall the exact error message now, it was late am yesterday. But that's exactly what I was doing. I was defining my method at the bottom of my code file, much as I would in a rails controller, sort of.
> 
> So that I am clear on the file extension stuff. And for your info to properly respond to my question; our servers run FreeBSD, Apache2, and on this testing platform, mod\_ruby. ? Do I need to use .cgi or .rb? Does it matter where the file is placed? Seemingly stupid questions, (and new to ruby) we know that a .cgi file is typically placed in the user\_web\_space cgi dir (or based on our tailored Apache config) to be executed. Would the same .rb file placed in the cgi dir be executed if called from the html? Would it need to be placed elsewhere? And/or are we talking about an Apache config mod? Was that clear as mud? Thanks greatly for your assistance. You've already been a great help.
> 
> tonyd

Hi Tony,

It really depends on your setup. If you want to use mod\_ruby, though, you will need to modify Apache's config file, and there it will be determined where your files will go and what extension you will use. If you do not use mod\_ruby, then your Ruby file can be run just like any other cgi program from your cgi directory.

-Justin
