Skip to content

Instantly share code, notes, and snippets.

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