From: Robert Klemme Date: 2009-04-09T01:10:11+09:00 Subject: Re: performing an action only the first time a function called On 08.04.2009 16:29, James French wrote: Please do not hijack other threads. > Could anyone suggest an elegant way of doing something like: > > $directXSDKBootstrapped = false > > def bootstrapDirectXSDK > if !$directXSDKBootstrapped > bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}") > $directXSDKBootstrapped = true > $directXSDKBootstrapped.freeze Freezing has no effect here because it does not prevent reassignment to the variable. > end > end > > Obviously the intention is to only do something the first time it is called Maybe some little reusable util?I have a number of different bootstrap functions I want to apply this to. Why can't you just invoke the initialization once and be done? If this is in a file which is required then Ruby will take care of this automatically. Another solution: class OnlyOnce def initialize(&b) raise "Need a block!" unless b @block = b end def get if @block @val = block.call @block = nil end @val end end $directXSDKBootstrapped = OnlyOnce.new do bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}") end Cheers robert