[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Instance Variable On-Change

Ryan Lewis

4/12/2008 4:30:00 PM

I need to make a way to run a method when class' instance variable
changes from false to true and only run it once. But I have no idea how
to do this.

Any ideas?
--
Posted via http://www.ruby-....

3 Answers

Tim Hunter

4/12/2008 5:18:00 PM

0

Ryan Lewis wrote:
> I need to make a way to run a method when class' instance variable
> changes from false to true and only run it once. But I have no idea how
> to do this.
>
> Any ideas?

Check out the observer library.

--
RMagick: http://rmagick.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

Philipp Hofmann

4/12/2008 6:35:00 PM

0

On Sun, Apr 13, 2008 at 02:17:37AM +0900, Tim Hunter wrote:
> Ryan Lewis wrote:
>> I need to make a way to run a method when class' instance variable
>> changes from false to true and only run it once. But I have no idea how
>> to do this.
>>
>> Any ideas?
>
> Check out the observer library.
>
>

why bother with observer if this can be nicely achieved with an
appropriate setter? something like ...

class SomeClass

def initialize
@switch = false
end

def switch=(value)
on_switch_becomes_true if !@switch && (@switch = value)
end

def on_switch_becomes_true
# ...
end

end

g phil

Tim Hunter

4/12/2008 7:06:00 PM

0

Philipp Hofmann wrote:
> On Sun, Apr 13, 2008 at 02:17:37AM +0900, Tim Hunter wrote:
>> Ryan Lewis wrote:
>>> I need to make a way to run a method when class' instance variable
>>> changes from false to true and only run it once. But I have no idea how
>>> to do this.
>>>
>>> Any ideas?
>> Check out the observer library.
>>
>>
>
> why bother with observer if this can be nicely achieved with an
> appropriate setter? something like ...
>

Good catch! I confess I assumed that the OP would not have asked if a
plain setter method would do the trick.


--
RMagick: http://rmagick.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html