# Class variables and how to set them up only once

**URL:** <https://www.ruby-forum.com/t/class-variables-and-how-to-set-them-up-only-once/110499>\
**Category:** Ruby\
**Created:** [July 26, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/class-variables-and-how-to-set-them-up-only-once/110499 "2007-07-26T21:31:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 26, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/class-variables-and-how-to-set-them-up-only-once/110499/1 "2007-07-26T21:31:20Z")

</div>

I’m trying to share a File object with everything that inherits from  
Superclass, for logging.

I’m trying to come up with a nice clean way of handling this, and this  
isn’t it:  
Right now it’s doing a begin/rescue/end to see if the @@logger.nil?  
(which throws an error if it doesn’t exist), and setup @@logger in the  
rescue.

The other thought I had was to take the setupLogger out of initialize,  
have a single dummy instance of SuperPage, and call setupLogger from  
that class once. That seems even worse.

I’m sure other people can think of more elegant ways of handling this,  
and I’d sure like to hear their suggestions 🙂

I tried putzing around with class\_variables, but I couldn’t seem to  
get at those from within a class.

–Kyle

class SuperPage  
def initialize()  
#lots of stuff  
setupLogger()#setsup an @@logger member that the log methods use  
ObjectSpace.define\_finalizer(self,  
SuperPage.create\_finalizer(@@logger))  
end

def logError(message)  
log(:Error,message)  
throw :assertion\_error  
end

def log(type,message)  
@@logger.puts("#{…ugly long string that includes the message…}")  
end

def setupLogger(path=“c:\#{DateTime.now.to\_s.gsub(’:’,’-’)}.xml”)  
begin  
@@logger.nil?  
rescue  
puts “I’m setting up the logger, you should only see me once”  
@@logger=File.new(“c:\#{DateTime.now.to\_s.gsub(’:’,’-’)}.xml”,“w”)  
@@logger.puts “”  
@loggerSetup=true  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 26, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/class-variables-and-how-to-set-them-up-only-once/110499/2 "2007-07-26T21:37:20Z")

</div>

And after I said I couldn’t find a way of getting at class\_variables  
from inside the class, the obvious occurred to me, whether or not it’s  
the ‘right’ way.  
self.class.class\_variables

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 26, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/class-variables-and-how-to-set-them-up-only-once/110499/3 "2007-07-26T21:40:21Z")

</div>

Right, so the way I wrote previously, or this way:

def setupLogger(path=“c:\#{DateTime.now.to\_s.gsub(’:’,’-’)}.xml”)  
unless self.class.class\_variables.include?"@@logger"  
puts “I’m setting up the logger, you should only see me once”  
@@logger=File.new(“c:\#{DateTime.now.to\_s.gsub(’:’,’-’)}.xml”,“w”)  
@@logger.puts “”  
@loggerSetup=true  
end  
end

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 26, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/class-variables-and-how-to-set-them-up-only-once/110499/4 "2007-07-26T22:23:28Z")

</div>

Hi –

On Fri, 27 Jul 2007, Kyle S. wrote:

> Right, so the way I wrote previously, or this way:
> 
> def setupLogger(path=“c:\#{DateTime.now.to\_s.gsub(’:’,’-’)}.xml”)  
> unless self.class.class\_variables.include?"@@logger"  
> puts “I’m setting up the logger, you should only see me once”  
> @@logger=File.new(“c:\#{DateTime.now.to\_s.gsub(’:’,’-’)}.xml”,“w”)  
> @@logger.puts “”  
> @loggerSetup=true  
> end  
> end

What about:

@@logger ||= File.new …

David

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 26, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/class-variables-and-how-to-set-them-up-only-once/110499/5 "2007-07-26T22:27:29Z")

</div>

On 26.07.2007 21:30, Kyle S. wrote:

> I’m trying to share a File object with everything that inherits from  
> Superclass, for logging.

> The other thought I had was to take the setupLogger out of initialize,  
> have a single dummy instance of SuperPage, and call setupLogger from  
> that class once. That seems even worse.
> 
> I’m sure other people can think of more elegant ways of handling this,  
> and I’d sure like to hear their suggestions 🙂

There is at least one logging package for Ruby (Log4R). I’d use that  
one instead of creating my own.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 26, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/class-variables-and-how-to-set-them-up-only-once/110499/6 "2007-07-26T22:40:30Z")

</div>

dblack,  
I’ve never tried to use ||= before… but damn if that  
doesn’t work well! I was expecting it to die like my @@whatever=:this  
unless @@whatever had when I tried that. Neat!

Robert,  
I’ve found the docs for log4r to be slightly better than  
those for well… umm. nothing. There about the worst docs I’ve found  
for a widely used package 😉 I’ve used log4r in other projects  
several months ago, but didn’t feel like going through the pain of  
setting it up and re-learning it. While it’s a good lib, the docs are  
horrible.

Thanks,  
Kyle
