Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created July 20, 2009 11:26
Show Gist options
  • Save lukeredpath/150261 to your computer and use it in GitHub Desktop.
Save lukeredpath/150261 to your computer and use it in GitHub Desktop.
require 'observable'
class ObservableArray
include Observable
def initialize(array = [])
@array = array
end
protected
def method_missing(method, *args, &block)
if @array.respond_to?(method)
current_hash = @array.hash
@array.send(method, *args, &block)
notify_changes if @array.hash != current_hash
else
super
end
end
private
def notify_changes
changed(true) and notify_observers(self)
end
end
# class MyObserver
# def update(array)
# puts "Array #{array} has changed"
# end
# end
#
# a = ObservableArray.new
# o = MyObserver.new
# a.add_observer(o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment