J
Joshua Ballanco
Hey all!
I'm still relatively new to Ruby, so forgive me if this seems like a
naive (or even stupid) question, but...
Is there a good method for implementing Key-Value Observing in Ruby?
Basically, what I'd like to be able to do is this:
class Actor
attr_accessor :var_to_change
def change_var
@var_to_change = 1
100.times {@var_to_change += 1}
end
end
class Observer
def initialize(act)
@actor = act
act.add_observer(self, var_to_change)
end
def observeValue(var, sender)
if sender == @actor
puts "The value is now #{var}"
end
end
end
act = Actor.new
obs = Observer.new(act)
thr = Thread.new {act.change_var}
thr.join
The resulting output would be:
The value is now 1
The value is now 2
...and so on.
I realize that there are all sorts of ways that similar functionality
could be achieved, but something about KVO is really attractive
(especially since Ruby is already really good at KVC). It seems to me
that this would be a fairly easy to implement as a module, but I don't
want to have to do the work if someone else already has. Does anyone
know how RubyCocoa handles KVO? or if it does?
Thanks for any help/comments/scornful rebukes. ;-)
-Josh
I'm still relatively new to Ruby, so forgive me if this seems like a
naive (or even stupid) question, but...
Is there a good method for implementing Key-Value Observing in Ruby?
Basically, what I'd like to be able to do is this:
class Actor
attr_accessor :var_to_change
def change_var
@var_to_change = 1
100.times {@var_to_change += 1}
end
end
class Observer
def initialize(act)
@actor = act
act.add_observer(self, var_to_change)
end
def observeValue(var, sender)
if sender == @actor
puts "The value is now #{var}"
end
end
end
act = Actor.new
obs = Observer.new(act)
thr = Thread.new {act.change_var}
thr.join
The resulting output would be:
The value is now 1
The value is now 2
...and so on.
I realize that there are all sorts of ways that similar functionality
could be achieved, but something about KVO is really attractive
(especially since Ruby is already really good at KVC). It seems to me
that this would be a fairly easy to implement as a module, but I don't
want to have to do the work if someone else already has. Does anyone
know how RubyCocoa handles KVO? or if it does?
Thanks for any help/comments/scornful rebukes. ;-)
-Josh