Lohan the observable


#1

Sorry, that name is a misnomer. However, I was excited to find that
Ruby
has a built in Observable module and I’m pretty bored, so I apologize in
advance…

require ‘observer’

one who is observed

class Celebrity
include Observable

attr_accessor :name
attr_reader :is

def is=(val)
@is = val
changed
notify_observers(self)
end
end

one who does the observering

class TrashMag
attr_accessor :name
def update(sender)
puts “#{name}'s Breaking News: OMG! #{sender.name} is #{sender.is}.
KeWl!!!”
end
end

celebs = []

lohan = Celebrity.new
lohan.name = “Lindsey Lohan”
celebs << lohan

britney = Celebrity.new
britney.name = “Britney Spears”
celebs << britney

paris = Celebrity.new
paris.name = “Paris Hilton”
celebs << paris

rags = [“People Magazine”,“US Weekly”,“InTouch”,“CNN”].each do |name|
mag = TrashMag.new
mag.name = name
celebs.each { |celeb| celeb.add_observer(mag) }
end

lohan.is = “trying to score coke while driving under the influence.”
britney.is = “attempting another comeback sans undergarments”
paris.is = “such a morally sound person now that she’s been through
jail”


#2

output:

People Magazine’s Breaking News: OMG! Lindsey Lohan is trying to score
coke
while under the influence… KeWl!!!
US Weekly’s Breaking News: OMG! Lindsey Lohan is trying to score coke
while
under the influence… KeWl!!!
InTouch’s Breaking News: OMG! Lindsey Lohan is trying to score coke
while
under the influence… KeWl!!!
CNN’s Breaking News: OMG! Lindsey Lohan is trying to score coke while
under
the influence… KeWl!!!
People Magazine’s Breaking News: OMG! Britney Spears is attempting
another
comeback . KeWl!!!
US Weekly’s Breaking News: OMG! Britney Spears is attempting another
comeback . KeWl!!!
InTouch’s Breaking News: OMG! Britney Spears is attempting another
comeback
. KeWl!!!
CNN’s Breaking News: OMG! Britney Spears is attempting another comeback
.
KeWl!!!
People Magazine’s Breaking News: OMG! Paris Hilton is such a stronger
person
now that she’s been through jail. KeWl!!!
US Weekly’s Breaking News: OMG! Paris Hilton is such a stronger person
now
that she’s been through jail. KeWl!!!
InTouch’s Breaking News: OMG! Paris Hilton is such a stronger person now
that she’s been through jail. KeWl!!!
CNN’s Breaking News: OMG! Paris Hilton is such a stronger person now
that
she’s been through jail. KeWl!!!