mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/observer] add symbol usage example to Observer#add_observer [doc]
https://github.com/ruby/observer/commit/62a94e3799
This commit is contained in:
parent
54acb3dd52
commit
e43d629575
1 changed files with 24 additions and 0 deletions
|
@ -111,6 +111,30 @@
|
||||||
# Current price: 112
|
# Current price: 112
|
||||||
# Current price: 79
|
# Current price: 79
|
||||||
# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 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
|
module Observable
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in a new issue