aboutsummaryrefslogtreecommitdiffstats
path: root/lib/observer.rb
diff options
context:
space:
mode:
authorTom Rothe <info@tomrothe.de>2020-06-21 17:12:34 +0200
committerMarc-Andre Lafortune <github@marc-andre.ca>2020-07-23 09:49:03 -0400
commite43d62957599ea6607ceb851b7bd9d90148de16b (patch)
treed21ec609e0896ac91e9212ee539e8be5a3816ea7 /lib/observer.rb
parent54acb3dd52a5fe75c32c3e36a6984d3ec4314a72 (diff)
downloadruby-e43d62957599ea6607ceb851b7bd9d90148de16b.tar.gz
[ruby/observer] add symbol usage example to Observer#add_observer [doc]
https://github.com/ruby/observer/commit/62a94e3799
Diffstat (limited to 'lib/observer.rb')
-rw-r--r--lib/observer.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/observer.rb b/lib/observer.rb
index acfe654301..0078d81449 100644
--- a/lib/observer.rb
+++ b/lib/observer.rb
@@ -111,6 +111,30 @@
# Current price: 112
# Current price: 79
# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79
+#
+# === Usage with procs
+#
+# The +#notify_observers+ method can also be used with +proc+s by using
+# the +:call+ as +func+ parameter.
+#
+# The following example illustrates the use of a lambda:
+#
+# require 'observer'
+#
+# class Ticker
+# include Observable
+#
+# def run
+# # logic to retrieve the price (here 77.0)
+# changed
+# notify_observers(77.0)
+# end
+# end
+#
+# ticker = Ticker.new
+# warner = ->(price) { puts "New price received: #{price}" }
+# ticker.add_observer(warner, :call)
+# ticker.run
module Observable
#