Created
May 14, 2015 14:59
-
-
Save StevenXL/a358360dbc4f5cf9ca38 to your computer and use it in GitHub Desktop.
dinosaurs
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 | |
private | |
def to_s | |
justification = 15 | |
puts "-" * 30 | |
puts "Name:".ljust(justification) + "#{@name}" if @name | |
puts "Period:".ljust(justification) + "#{@period}" if @period | |
puts "Continent:".ljust(justification) + "#{@continent}" if @continent | |
puts "Diet:".ljust(justification) + "#{@diet}" if @diet | |
puts "Weight:".ljust(justification) + "#{@weight}" if @weight | |
puts "Locomotion:".ljust(justification) + "#{@locomotion}" if @locomotion | |
puts "Description:".ljust(justification) + "#{@description}" if @description | |
end | |
end | |
class Dinodex | |
def initialize(dinosaurs = []) | |
@dinosaurs = dinosaurs | |
end | |
def <<(dino) | |
@dinosaurs << dino | |
end | |
#TODO: Implement display one dinosaur or all dinosaurs | |
def display | |
to_s | |
end | |
def filter(type) | |
case type | |
when "biped" | |
bipeds | |
when "carnivore" | |
carnivores | |
when "big" | |
biggest | |
when "small" | |
smallest | |
end | |
end | |
private | |
def to_s | |
@dinosaurs.each do |dino| | |
puts dino | |
end | |
puts "-" * 30 | |
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 | |
dinodex.display |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment