I have to admit though, I don't understand the 'wrapped, count =3D self, 0'=
line...
-----Original Message-----
From: James French [mailto:
[email protected]]=20
Sent: 08 April 2009 16:13
To: ruby-talk ML
Subject: Re: performing an action only the first time a function called
I see I still have some way to go with ruby
Nice! Can't believe how quic=
k you knocked that up...
-----Original Message-----
From: James Coglan [mailto:
[email protected]]=20
Sent: 08 April 2009 15:43
To: ruby-talk ML
Subject: Re: performing an action only the first time a function called
Could anyone suggest an elegant way of doing something like:
$directXSDKBootstrapped =3D false
def bootstrapDirectXSDK
if !$directXSDKBootstrapped
bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
$directXSDKBootstrapped =3D true
$directXSDKBootstrapped.freeze
end
end
One way of doing this is as follows:
class Proc
def runs(n)
wrapped, count =3D self, 0
lambda { |*args|
count +=3D 1
count <=3D n ? wrapped.call(*args) : nil
}
end
end
foo =3D lambda { |arg| puts arg }.runs(3)
#=3D> # said:
look!
=3D> nil
look!
=3D> nil
look!
=3D> nil
=3D> nil
Notice nothing is printed the fourth time. So using this, you could write
you function as:
bootstrapDirectXSDK =3D lambda {
bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
}.runs(1)
Which will only allow its contents to be executed once.