[前][次][番号順一覧][スレッド一覧]

ruby-changes:62385

From: Tom <ko1@a...>
Date: Thu, 23 Jul 2020 22:49:46 +0900 (JST)
Subject: [ruby-changes:62385] e43d629575 (master): [ruby/observer] add symbol usage example to Observer#add_observer [doc]

https://git.ruby-lang.org/ruby.git/commit/?id=e43d629575

From e43d62957599ea6607ceb851b7bd9d90148de16b Mon Sep 17 00:00:00 2001
From: Tom Rothe <info@t...>
Date: Sun, 21 Jun 2020 17:12:34 +0200
Subject: [ruby/observer] add symbol usage example to Observer#add_observer
 [doc]

https://github.com/ruby/observer/commit/62a94e3799

diff --git a/lib/observer.rb b/lib/observer.rb
index acfe654..0078d81 100644
--- a/lib/observer.rb
+++ b/lib/observer.rb
@@ -111,6 +111,30 @@ https://github.com/ruby/ruby/blob/trunk/lib/observer.rb#L111
 #   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
 
   #
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]