{"id":15032811,"url":"https://github.com/ruby/observer","last_synced_at":"2025-04-05T14:08:10.108Z","repository":{"id":42977434,"uuid":"200772432","full_name":"ruby/observer","owner":"ruby","description":"The Observer pattern provides a simple mechanism for one object to inform a set of interested third-party objects when its state changes.","archived":false,"fork":false,"pushed_at":"2023-11-07T03:42:57.000Z","size":52,"stargazers_count":36,"open_issues_count":0,"forks_count":9,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-03-29T13:09:13.547Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ruby.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-06T03:51:08.000Z","updated_at":"2025-02-13T09:21:44.000Z","dependencies_parsed_at":"2024-06-18T22:54:54.854Z","dependency_job_id":"c5b0c7eb-1203-42d9-beb1-473a11c004d1","html_url":"https://github.com/ruby/observer","commit_stats":{"total_commits":48,"total_committers":17,"mean_commits":2.823529411764706,"dds":0.625,"last_synced_commit":"6c978e6196b33405aced08ca3c3a5600f5b271e5"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Fobserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Fobserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Fobserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Fobserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ruby","download_url":"https://codeload.github.com/ruby/observer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247345853,"owners_count":20924102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T20:19:29.388Z","updated_at":"2025-04-05T14:08:10.003Z","avatar_url":"https://github.com/ruby.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Observer\n\nThe Observer pattern (also known as publish/subscribe) provides a simple\nmechanism for one object to inform a set of interested third-party objects\nwhen its state changes.\n\n## Mechanism\n\nThe notifying class mixes in the +Observable+\nmodule, which provides the methods for managing the associated observer\nobjects.\n\nThe observable object must:\n\n* assert that it has +#changed+\n* call +#notify_observers+\n\nAn observer subscribes to updates using Observable#add_observer, which also\nspecifies the method called via #notify_observers. The default method for\nnotify_observers is #update.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'observer'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install observer\n\n## Usage\n\nThe following example demonstrates this nicely.  A +Ticker+, when run,\ncontinually receives the stock +Price+ for its \u003ctt\u003e@symbol\u003c/tt\u003e.  A +Warner+\nis a general observer of the price, and two warners are demonstrated, a\n+WarnLow+ and a +WarnHigh+, which print a warning if the price is below or\nabove their set limits, respectively.\n\nThe +update+ callback allows the warners to run without being explicitly\ncalled.  The system is set up with the +Ticker+ and several observers, and the\nobservers do their duty without the top-level code having to interfere.\n\nNote that the contract between publisher and subscriber (observable and\nobserver) is not declared or enforced.  The +Ticker+ publishes a time and a\nprice, and the warners receive that.  But if you don't ensure that your\ncontracts are correct, nothing else can warn you.\n\n```ruby\nrequire \"observer\"\n\nclass Ticker          ### Periodically fetch a stock price.\n\n  include Observable\n\n  def initialize(symbol)\n    @symbol = symbol\n  end\n\n  def run\n    last_price = nil\n    loop do\n      price = Price.fetch(@symbol)\n      print \"Current price: #{price}\\n\"\n      if price != last_price\n        changed                 # notify observers\n        last_price = price\n        notify_observers(Time.now, price)\n      end\n      sleep 1\n    end\n  end\nend\n\nclass Price           ### A mock class to fetch a stock price (60 - 140).\n  def self.fetch(symbol)\n    60 + rand(80)\n  end\nend\n\nclass Warner          ### An abstract observer of Ticker objects.\n  def initialize(ticker, limit)\n    @limit = limit\n    ticker.add_observer(self)\n  end\nend\n\nclass WarnLow \u003c Warner\n  def update(time, price)       # callback for observer\n    if price \u003c @limit\n      print \"--- #{time.to_s}: Price below #@limit: #{price}\\n\"\n    end\n  end\nend\n\nclass WarnHigh \u003c Warner\n  def update(time, price)       # callback for observer\n    if price \u003e @limit\n      print \"+++ #{time.to_s}: Price above #@limit: #{price}\\n\"\n    end\n  end\nend\n\nticker = Ticker.new(\"MSFT\")\nWarnLow.new(ticker, 80)\nWarnHigh.new(ticker, 120)\nticker.run\n```\n\nProduces:\n\n```\nCurrent price: 83\nCurrent price: 75\n--- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 75\nCurrent price: 90\nCurrent price: 134\n+++ Sun Jun 09 00:10:25 CDT 2002: Price above 120: 134\nCurrent price: 134\nCurrent price: 112\nCurrent price: 79\n--- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/ruby/observer.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby%2Fobserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruby%2Fobserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby%2Fobserver/lists"}