Skip to content

Instantly share code, notes, and snippets.

@kibiz0r
Created March 24, 2011 01:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kibiz0r/884416 to your computer and use it in GitHub Desktop.
Save kibiz0r/884416 to your computer and use it in GitHub Desktop.
require 'ostruct'
class HockeyClient
def listen_for_hockey_scores
sleep 1
yield score "Blues", "Cracknell"
sleep 0.1
yield score "Red Wings", "Salei"
sleep 0.1
yield score "Red Wings", "Helm"
sleep 1
yield score "Red Wings", "Kronwall"
sleep 0.1
yield score "Blues", "Backes"
sleep 0.2
yield score "Blues", "D'Agostini"
sleep 0.2
yield score "Red Wings", "Datsyuk"
sleep 0.1
yield score "Red Wings", "Abdelkader"
end
private
def score(team, scorer)
OpenStruct.new :team => team, :scorer => scorer
end
end
if $0 == __FILE__
hockey_client = HockeyClient.new
puts "Listening to the game..."
hockey_client.listen_for_hockey_scores do |goal|
puts "#{goal.scorer} scored a goal for #{goal.team}"
end
puts "The game is over."
end
require './hockey_client'
class RedWingsFan
def initialize(hockey_client)
@hockey_client = hockey_client
end
def red_wings_goal_scorers
@hockey_client.enum_for(:listen_for_hockey_scores).select do |goal|
goal.team == "Red Wings"
end.map do |goal|
goal.scorer
end
end
end
red_wings_fan = RedWingsFan.new(HockeyClient.new)
puts "Red Wings scorers:"
red_wings_fan.red_wings_goal_scorers.each do |scorer|
puts scorer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment