Skip to content

Instantly share code, notes, and snippets.

@framallo
Created December 11, 2015 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save framallo/bddeb044e5fc17b084aa to your computer and use it in GitHub Desktop.
Save framallo/bddeb044e5fc17b084aa to your computer and use it in GitHub Desktop.
require_relative 'video_game'
require 'pp'
wow = VideoGame.new(types: ['rpg'], genre: :mmo, name: 'World of warcraft')
pp wow.types # => ['rpg']
wow.types = []
pp wow.types # => ['rpg']
puts 'game2'
game2 = VideoGame.new(types: ['rpg'], genre: :mmo, name: 'World of warcraft')
pp game2.genre
puts 'print title'
pp game2.title
wow = VideoGame.new(types: ['rpg', 'fps', 'strategy'], genre: 'mmo', name: 'World of warcraft')
puts wow
puts wow.title
require 'pp'
class VideoGame
def self.types
{ mmo: 'Massively multi player' }
end
def initialize(attributes)
self.name = attributes[:name]
self.types = attributes[:types]
self.genre = attributes[:genre]
end
attr_accessor :types, :name, :genre
def types=(types)
raise 'types should be an array' unless types.is_a? Array
@types = types
end
def types
@types ||=[]
@types.shuffle
end
def genre=(genre)
genre = genre.to_sym
raise 'invalid genre' unless VideoGame.types.keys.include?(genre)
@genre = genre
end
def genre_name
VideoGame.types[genre]
end
def title
"#{name} genre:#{genre_name} and types: #{types.join(', ')}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment