Created
December 11, 2015 01:12
-
-
Save framallo/bddeb044e5fc17b084aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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