Skip to content

Instantly share code, notes, and snippets.

@StevenXL
Created May 14, 2015 15:04
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 StevenXL/baf769f84aa3665d149e to your computer and use it in GitHub Desktop.
Save StevenXL/baf769f84aa3665d149e to your computer and use it in GitHub Desktop.
dinosaur_3
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
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|
dino.to_s
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
bipeds = dinodex.filter("biped")
bipeds.display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment