David Garamond wrote:
> When writing short scripts (several pages long) I often want to share 
> some variables (like CGI object, DB connection object) in several 
> methods. I often use globals for this:
> 
>  $cgi = CGI.new('html3')
>  $conn = PGconn.connect(...)
> 
>  def foo
>    $conn.exec(...)
>    $cgi.out { ... }
>  end
> 
> When I don't want to see all those dollar signs, I use a wrapper method:
> 
>  $_cgi = CGI.new('html3')
>  def cgi; $_cgi end
> 
>  def foo
>    cgi.out { ... }
>  end
> 
> But it still looks ugly to me. Any suggestion to make it more elegant?
> 

For really short scripts, I usually pass the "globals" as parameters to 
the methods. If the scripts get long enough, to where that's an 
annoyance, I take that as a hint that its time to add some classes. 
Then, what used to be globals become instance variables of the class, 
and you can use "attr_reader" to easily create your accessor methods for 
you (if the '@' sigil bothers you).

- Jamis


-- 
Jamis Buck
jgb3 / email.byu.edu
http://www.jamisbuck.org/jamis