Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
Class variables and how to set them up only once.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Kyle Schmitt, post: 4569107"] 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 "<log>" @loggerSetup=true end end end [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Class variables and how to set them up only once.
Top