# CGI and Methods?

**URL:** <https://www.ruby-forum.com/t/cgi-and-methods/132219>\
**Category:** Ruby\
**Created:** [March 18, 2008, 12:27am UTC](https://www.ruby-forum.com/t/cgi-and-methods/132219 "2008-03-18T00:27:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonydm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@tonydm](https://www.ruby-forum.com/u/tonydm)\
**Post date:** [March 18, 2008, 12:27am UTC](https://www.ruby-forum.com/t/cgi-and-methods/132219/1 "2008-03-18T00:27:15Z")

</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?  
puts “Content-type: text/plain\r\n\r\n\n\nCommSpeed  
Log Search\n\n\n”  
puts “

# Please enter a search criteria
”  
puts “\n\n”  
exit  
end
# A hash of possible logs

logs = {‘smtp’ =\> ‘smtp’, ‘mail’ =\> ‘maillog’, ‘mx’ =\> ‘mx’, ‘spam’ =\>  
‘spam’, ‘Dial Up’ =\> ‘radius’, ‘DHCP’ =\> ‘dhcp’ }

if smethod == “CAT”

# Array to hold our search result matches

results = Array.new

# Open file and look for our search string

File.open("/var/log/"+logs[slogtype],  
‘r’).grep(/#{search\_string}/).each do |line|  
results.push(line) # append it to our results array  
end # file is automatically closed

if results.empty?  
# No matches found  
puts “Content-type:  
text/plain\r\n\r\n\n\nCommSpeed Log  
Search\n\n\n”  
puts “

# No DHCP Log Results Found!
”  
puts “\n\n”  
exit  
else  
# Now send results back to browser  
puts “Content-type:  
text/plain\r\n\r\n\n\nCommSpeed Log  
Search\n\n\n”  
puts “
# DHCP Log Results…
”

```
  # Print the results
  results.each do |line|
    puts line+"<br>"
  end

# Close HTML
puts "</BODY>\n</HTML>\n"
            exit

```

end  
end

if smethod == “TAIL”  
#TODO  
#Need to figure out how to tail a file with our search scope in place  
and feed it to the browser window.  
end

---

<div class="post-metadata">

**Author:** ![tonydm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@tonydm](https://www.ruby-forum.com/u/tonydm)\
**Post date:** [March 18, 2008, 3:15am UTC](https://www.ruby-forum.com/t/cgi-and-methods/132219/2 "2008-03-18T03:15:33Z")

</div>

Tony De wrote:

> aid me in my learning process. Thank you very much.
> 
> tonyd

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.

> ```
> puts "Content-type:
> 
> ```
> 
> text/plain\r\n\r\n\n\nCommSpeed Log  
> Search\n\n\n"  
> puts “
> 
> # No DHCP Log Results Found!
> ”  
> puts “\n\n”  
> 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:** ![tonydm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@tonydm](https://www.ruby-forum.com/u/tonydm)\
**Post date:** [March 18, 2008, 4:58am UTC](https://www.ruby-forum.com/t/cgi-and-methods/132219/3 "2008-03-18T04:58:51Z")

</div>

Justin C. wrote:

> Tony De wrote:
> 
> > aid me in my learning process. Thank you very much.
> > 
> > tonyd
> 
> 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.
> 
> > ```
> > puts "Content-type:
> > 
> > ```
> > 
> > text/plain\r\n\r\n\n\nCommSpeed Log  
> > Search\n\n\n"  
> > puts “
> > 
> > # No DHCP Log Results Found!
> > ”  
> > puts “\n\n”  
> > 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

---

<div class="post-metadata">

**Author:** ![tonydm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@tonydm](https://www.ruby-forum.com/u/tonydm)\
**Post date:** [March 18, 2008, 10:01am UTC](https://www.ruby-forum.com/t/cgi-and-methods/132219/4 "2008-03-18T10:01:02Z")

</div>

Justin C. wrote:

> Tony De wrote:
> 
> > > you can have any methods, classes, or other code that you want.  
> > > about the middle of the page here:
> > 
> > tonyd
> 
> 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:
> 
> def my\_method  
> end
> 
> my\_method
> 
> will work, but
> 
> my\_method
> 
> def my\_method  
> 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

---

<div class="post-metadata">

**Author:** ![tonydm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@tonydm](https://www.ruby-forum.com/u/tonydm)\
**Post date:** [March 18, 2008, 9:23am UTC](https://www.ruby-forum.com/t/cgi-and-methods/132219/5 "2008-03-18T09:23:13Z")

</div>

Tony De wrote:

> > you can have any methods, classes, or other code that you want.  
> > about the middle of the page here:
> 
> tonyd

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:

def my\_method  
end

my\_method

will work, but

my\_method

def my\_method  
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:** ![tonydm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@tonydm](https://www.ruby-forum.com/u/tonydm)\
**Post date:** [March 18, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/cgi-and-methods/132219/6 "2008-03-18T17:38:19Z")

</div>

Tony De wrote:

> > It does matter where you place the method definition, in a way. That is,
> 
> properly respond to my question; our servers run FreeBSD, Apache2, and

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
