Created
May 14, 2015 15:56
-
-
Save StevenXL/6ff6739663c06eaef67e to your computer and use it in GitHub Desktop.
Dinosaur 5
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 'csv' | |
class Dinosaur | |
attr_reader :name, :period, :continent, :diet, :weight, :locomotion, | |
:description | |
def initialize(name, period, continent, diet, weight, locomotion, description) | |
@name = name | |
@period = period | |
@continent = continent | |
@diet = diet | |
@weight = weight | |
@locomotion = locomotion | |
@description = description | |
end | |
def biped | |
@locomotion == "Biped" | |
end | |
def carnivorous | |
carnivorous = ["Yes", "Carnivore", "Insectivore", "Piscivore"] | |
carnivorous.include?(@diet) | |
end | |
def big | |
@weight > 2000 ? true : false | |
end | |
def small | |
not big | |
end | |
def to_s | |
justification = 15 | |
representation = '' | |
representation << "-" * 30 + "\n" | |
representation << "Name:".ljust(justification) + "#{@name}\n" if @name | |
representation << "Period:".ljust(justification) + "#{@period}\n" if @period | |
representation << "Continent:".ljust(justification) + "#{@continent}\n" if @continent | |
representation << "Diet:".ljust(justification) + "#{@diet}\n" if @diet | |
representation << "Weight:".ljust(justification) + "#{@weight}\n" if @weight | |
representation << "Locomotion:".ljust(justification) + "#{@locomotion}\n" if @locomotion | |
representation << "Description:".ljust(justification) + "#{@description}\n" if @description | |
representation | |
end | |
end | |
class Dinodex | |
def initialize(dinosaurs = []) | |
@dinosaurs = dinosaurs | |
end | |
def <<(dino) | |
@dinosaurs << dino | |
end | |
#TODO: Implement display one dinosaur or all dinosaurs | |
def filter(type) | |
case type | |
when "biped" | |
bipeds | |
when "carnivore" | |
carnivores | |
when "big" | |
biggest | |
when "small" | |
smallest | |
end | |
end | |
private | |
def to_s | |
string_rep = '' | |
@dinosaurs.each do |dino| | |
string_rep << dino.to_s | |
end | |
string_rep << "-" * 30 | |
string_rep | |
end | |
def bipeds | |
selection = @dinosaurs.select { |dino| dino.biped } | |
Dinodex.new(selection) | |
end | |
def carnivores | |
selection = @dinosaurs.select { |dino| dino.carnivorous } | |
Dinodex.new(selection) | |
end | |
def biggest | |
selection = @dinosaurs.select { |dino| dino.big } | |
Dinodex.new(selection) | |
end | |
def smallest | |
selection = @dinosaurs.select { |dino| dino.small } | |
Dinodex.new(selection) | |
end | |
end | |
# Load Data Into dinodex | |
dinodex = Dinodex.new | |
OPTIONS = { :headers => true, :converters => :all } | |
CSV.foreach("dinodex.csv", OPTIONS) do |row| | |
name = row["NAME"] | |
period = row["PERIOD"] | |
continent = row["CONTINENT"] | |
diet = row["DIET"] | |
weight = row["WEIGHT_IN_LBS"] | |
locomotion = row["WALKING"] | |
description = row["DESCRIPTION"] | |
dinodex << Dinosaur.new(name, period, continent, diet, weight, locomotion, | |
description) | |
end | |
bipeds = dinodex.filter("biped") | |
puts bipeds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment