Skip to content

Instantly share code, notes, and snippets.

@poysama
Created November 27, 2012 06:44
Show Gist options
  • Save poysama/4152777 to your computer and use it in GitHub Desktop.
Save poysama/4152777 to your computer and use it in GitHub Desktop.
improve method list
#!/usr/bin/env ruby
class Game
attr_accessor :name, :year, :system
attr_reader :created_at
def initialize(name, options={})
self.name = name
self.year = options[:year]
self.system = options[:system]
@created_at = Time.now
end
end
class Library
attr_accessor :games
def initialize(games)
@games = games
end
def list
if block_given?
games.each do |game|
out = yield game
puts out
end
else
games.each do |game|
puts game.name
end
end
end
end
GAMES = [
Game.new('Contra', year: 1987, system: 'NES'),
Game.new('Civilization', year: 1991, system: 'PC'),
Game.new('The Legend of Zelda', year: 1986, system: 'NES'),
Game.new('Mega Man X2', year: 1995, system: 'SNES'),
Game.new('Super Metroid', year: 1994, system: 'SNES'),
Game.new('Sim City 2000', year: 1993, system: 'PC'),
Game.new('Starcraft', year: 1998, system: 'PC')
]
library = Library.new(GAMES)
puts "\n\nNo Block"
library.list
puts "\n\nBlock"
library.list { |game| game.year }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment