From: "Ara.T.Howard" Date: 2005-11-17T00:35:44+09:00 Subject: Re: static variable; behaviour in ruby? On Thu, 17 Nov 2005, Hugh Sasse wrote: > Maybe something like > def my_method x, y, z, &block > state = rand # do something with x, y, z > alias :old_my_method, :my_method > define_method(:my_method)(x,y,z, &block) > lambda{ block[x, y, z] if state < 0.42 }.call > end > end but the second time my_method is called you'll clobber old_my_method - is that what you want? plus it only works once because the first call to my_method will define it in such a way that it never sets of state again - it's clobbered by the define_method/lambda bit... > might do the trick, then? The state would get carried around with the > closure in subesequent calls to the same method? Except having sevaral > calls in the same block would be treated the same as severl calls to the > same method, rather than the way sed treats /this/,/that/{ statements}, each > heing independent, runnin for each line of the input. i really don't quite get what you are after. but from what i'm reading it looks alot like you would be better off doing something like this class Line def initialize x, y, z, &statements @state = rand @x, @y, @z = x, y, z @statements = statements end def my_method @statements[@x, @y, @z] if @state < 0.42 end end put another way - a static variable can be emulated as an instance variable of an object. you can have multiple copies by having multiple instances. the initialize methods makes sure it's only called once. this might be easier? -a -- =============================================================================== | ara [dot] t [dot] howard [at] gmail [dot] com | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | -- bodhicaryavatara ===============================================================================