Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#only_once(*args) ⇒ Object

Do something, but only once (in the lifetime of the program). This is based on the output of caller() and the value of args, so there’s some possibility for error if you’ve reloaded files, etc. Additionally, you should be strongly cautioned that objects given as arguments to only_once() will persist in memory until program termination.



9
10
11
12
13
14
15
# File 'lib/only_once.rb', line 9

def only_once(*args)
	$only_once_previously_called_from ||= Array.new
	unless $only_once_previously_called_from.include? [caller[0], args]
		$only_once_previously_called_from << [caller[0], args]
		yield
	end
end