[colug-432] ruby class variable behavior?

Rick Hornsby richardjhornsby at gmail.com
Tue Jan 12 14:17:26 EST 2016


I'm not sure how class variables, @@, behave in Ruby.  I'm using them sparingly.  One place I do need to use a class variable is part of an auditing script.  I need to load an exceptions file.

It doesn't make sense to try to load it every time an instance of the class is created.  That's a lot of extra file i/o, and maybe wasted memory?  I'm not entirely sure, however, if what I intend to happen is happening.  The script works fine, I'm more wondering about this:

class myClass
	@@audit_exceptions = YAML::load_file('exceptions.yml')
	
	def initialize
	...
	end
end

Is the exceptions.yml file loaded each time a new instance of the class is created, and it just overwrites the @@audit_exceptions class variable?  Or is it loaded just once the first time myClass.new is invoked?

sidenote: if there is a better way to handle what I'm trying to do, I'm open to suggestions.

thanks!


More information about the colug-432 mailing list