Created
May 13, 2015 21:53
-
-
Save StevenXL/1cde7bb1e1829c439d3a to your computer and use it in GitHub Desktop.
dinosaur 2
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 | |
end | |
class Dinodex | |
def initialize(dinosaurs = []) | |
@dinosaurs = dinosaurs | |
end | |
def <<(dino) | |
@dinosaurs << dino | |
end | |
def to_s | |
@dinosaurs.to_s | |
end | |
#TODO: Implement display one dinosaur or all dinosaurs | |
def display | |
end | |
def filter(type) | |
case type | |
when "biped" | |
bipeds | |
when "carnivore" | |
carnivores | |
when "big" | |
biggest | |
when "small" | |
smallest | |
end | |
end | |
private | |
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 | |
puts dinodex.filter("biped") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment