Instance Variable On-Change

R

Ryan Lewis

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?
 
T

Tim Hunter

Ryan said:
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.
 
P

Philipp Hofmann

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
 
T

Tim Hunter

Philipp said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,290
Messages
2,571,452
Members
48,129
Latest member
DianneCarn

Latest Threads

Top