Skip to main content
Notice removed Draw attention by RubberDuck
Bounty Ended with Devon Parsons's answer chosen by RubberDuck
Notice added Draw attention by RubberDuck
Bounty Started worth 100 reputation by RubberDuck
Updated Code
Source Link
Steven L.
  • 425
  • 2
  • 8
require 'csv'

class Dinosaur
  attr_reader :name, :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 carnivorous
    carnivorous = ["Yes", "Carnivore", "Insectivore", "Piscivore"]
    carnivorous.include?(@diet)
  end

  def period(timespan)
    @period.downcase.include?(timespan.downcase)
  end

  def bigsize
    (@weight > 2000 ? true :return false)nil if @weight.nil?

  end

  def@weight small
> 2000 ? "big" not: big"small"
  end 


  def to_s
    justification = 15

    representation = ''

    representation << "-" * 3099 + "\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

  def filter(options = {})
    selection = @dinosaurs

    options.each do |param, value|
      case param
      when "period"
        selection = selection.select { |dino| dino.send(param, value) }
      when "big"
        selection = selection.select { |dino| dino.big }
      when "small"
        selection = selection.select { |dino| dino.small }
      when "carnivore"
        selection = selection.select { |dino| dino.carnivorecarnivorous }
      else
        selection = selection.select { |dino| dino.send(param) == value }
      end
    end

    Dinodex.new(selection)
  end

  private

  def to_s
    string_rep = ''

    @dinosaurs.each do |dino|
      string_rep << dino.to_s
    end

    string_rep << "-" * 3099

    string_rep
  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

CSV.foreach("african_dinosaur_export.csv", OPTIONS) do |row|
  name = row["Genus"]
  period = row["Period"]
  continent = row["Continent"]
  diet = "Carnivore" if row["Carnivore"] == "Yes"
  weight =  row["Weight"]
  locomotion = row["Walking"]
  description = nil

  dinodex << Dinosaur.new(name, period, continent, diet, weight, locomotion,
                          description)
end

# puts dinodex
puts dinodex.filter({"size" "period"=> "big", "diet" => "Late""Carnivore"})
require 'csv'

class Dinosaur
  attr_reader :name, :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 carnivorous
    carnivorous = ["Yes", "Carnivore", "Insectivore", "Piscivore"]
    carnivorous.include?(@diet)
  end

  def period(timespan)
    @period.downcase.include?(timespan.downcase)
  end

  def big
    (@weight > 2000 ? true : false) if @weight
  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

  def filter(options = {})
    selection = @dinosaurs

    options.each do |param, value|
      case param
      when "period"
        selection = selection.select { |dino| dino.send(param, value) }
      when "big"
        selection = selection.select { |dino| dino.big }
      when "small"
        selection = selection.select { |dino| dino.small }
      when "carnivore"
        selection = selection.select { |dino| dino.carnivore }
      else
        selection = selection.select { |dino| dino.send(param) == value }
      end
    end

    Dinodex.new(selection)
  end

  private

  def to_s
    string_rep = ''

    @dinosaurs.each do |dino|
      string_rep << dino.to_s
    end

    string_rep << "-" * 30

    string_rep
  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({ "period" => "Late"})
require 'csv'

class Dinosaur
  attr_reader :name, :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 carnivorous
    carnivorous = ["Yes", "Carnivore", "Insectivore", "Piscivore"]
    carnivorous.include?(@diet)
  end

  def period(timespan)
    @period.downcase.include?(timespan.downcase)
  end

  def size
    return nil if @weight.nil?

    @weight > 2000 ? "big" : "small"
  end 


  def to_s
    justification = 15

    representation = ''

    representation << "-" * 99 + "\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

  def filter(options = {})
    selection = @dinosaurs

    options.each do |param, value|
      case param
      when "period"
        selection = selection.select { |dino| dino.send(param, value) }
      when "carnivore"
        selection = selection.select { |dino| dino.carnivorous }
      else
        selection = selection.select { |dino| dino.send(param) == value }
      end
    end

    Dinodex.new(selection)
  end

  private

  def to_s
    string_rep = ''

    @dinosaurs.each do |dino|
      string_rep << dino.to_s
    end

    string_rep << "-" * 99

    string_rep
  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

CSV.foreach("african_dinosaur_export.csv", OPTIONS) do |row|
  name = row["Genus"]
  period = row["Period"]
  continent = row["Continent"]
  diet = "Carnivore" if row["Carnivore"] == "Yes"
  weight =  row["Weight"]
  locomotion = row["Walking"]
  description = nil

  dinodex << Dinosaur.new(name, period, continent, diet, weight, locomotion,
                          description)
end

# puts dinodex
puts dinodex.filter({"size" => "big", "diet" => "Carnivore"})
Tweeted twitter.com/#!/StackCodeReview/status/599021257041047552
Formatting
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90

First, I created a DinosaurDinosaur class. Most of the attributes are readable (except for the @period@period instance variable). I've created a custom Dinosaur#to_sDinosaur#to_s method in order to print the dinosaur object. I've also created Dinosaur#carnivorousDinosaur#carnivorous, Dinosaur#bigDinosaur#big, and Dinosaur#smallDinosaur#small. These methods return Booleans, but I didn't name them with an ending "?" for reasons that might be clear in the implementation of the filter method.

Secondly, I created a DinodexDinodex class. This class holds DinosaurDinosaur objects in an array. Because the internal data structure is an array, I've defined Dinodex#<<(Dinosaur)Dinodex#<<(Dinosaur). Dinodex#to_sDinodex#to_s merely aggregates the strings returned calls to Dinosaur#to_sDinosaur#to_s, for all the dinosaurs in the dinodex.

The interesting part about the Dinodex Dinodex (or at least the one I had the most trouble with), is the Dinodex#filter methodDinodex#filter method. The Dinodex#filterDinodex#filter method takes an options hash, where (ideally) the keys are attributes of the DinosaurDinosaur class, and the values are what the user is selecting for.

NOTE: I am just realizing now that I am writing this out that the else branch in the Dinodex#filterDinodex#filter method that I should check if param is valid. I would do this by using the respond_to?(param) method.

I had a tough time figuring out how to make the Dinodex#filtersDinodex#filters method recursive, until I realized I can just update what the local variable selection points to. Once the right dinosaurs have been selected, a new DinodexDinodex object is returned, so that chaining is possible.

First, I created a Dinosaur class. Most of the attributes are readable (except for the @period instance variable). I've created a custom Dinosaur#to_s method in order to print the dinosaur object. I've also created Dinosaur#carnivorous, Dinosaur#big, and Dinosaur#small. These methods return Booleans, but I didn't name them with an ending "?" for reasons that might be clear in the implementation of the filter method.

Secondly, I created a Dinodex class. This class holds Dinosaur objects in an array. Because the internal data structure is an array, I've defined Dinodex#<<(Dinosaur). Dinodex#to_s merely aggregates the strings returned calls to Dinosaur#to_s, for all the dinosaurs in the dinodex.

The interesting part about the Dinodex (or at least the one I had the most trouble with), is the Dinodex#filter method. The Dinodex#filter method takes an options hash, where (ideally) the keys are attributes of the Dinosaur class, and the values are what the user is selecting for.

NOTE: I am just realizing now that I am writing this out that the else branch in the Dinodex#filter method that I should check if param is valid. I would do this by using the respond_to?(param) method.

I had a tough time figuring out how to make the Dinodex#filters method recursive, until I realized I can just update what the local variable selection points to. Once the right dinosaurs have been selected, a new Dinodex object is returned, so that chaining is possible.

First, I created a Dinosaur class. Most of the attributes are readable (except for the @period instance variable). I've created a custom Dinosaur#to_s method in order to print the dinosaur object. I've also created Dinosaur#carnivorous, Dinosaur#big, and Dinosaur#small. These methods return Booleans, but I didn't name them with an ending "?" for reasons that might be clear in the implementation of the filter method.

Secondly, I created a Dinodex class. This class holds Dinosaur objects in an array. Because the internal data structure is an array, I've defined Dinodex#<<(Dinosaur). Dinodex#to_s merely aggregates the strings returned calls to Dinosaur#to_s, for all the dinosaurs in the dinodex.

The interesting part about the Dinodex (or at least the one I had the most trouble with), is the Dinodex#filter method. The Dinodex#filter method takes an options hash, where (ideally) the keys are attributes of the Dinosaur class, and the values are what the user is selecting for.

NOTE: I am just realizing now that I am writing this out that the else branch in the Dinodex#filter method that I should check if param is valid. I would do this by using the respond_to?(param) method.

I had a tough time figuring out how to make the Dinodex#filters method recursive, until I realized I can just update what the local variable selection points to. Once the right dinosaurs have been selected, a new Dinodex object is returned, so that chaining is possible.

deleted 39 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I would like you to comment on how well you think I met the requirements with the exception of 1) loading the african dinosaur data and 2) exporting the data to JSON. (The reason I have not completed these is because I am pretty sure I can do the first one, and I need to do further homework before I can do the second one).

  1. loading the African dinosaur data
  2. exporting the data to JSON (the reason I have not completed these is because I am pretty sure I can do the first one, and I need to do further homework before I can do the second one)

I had a tough time figuring out how to make the Dinodex#filters method recursive, until I realized I can just update what the local variable selection points to. Once the right dinosaurs have been selected, a new Dinodex object is returned, so that chaining is possible.

OK, the code is this:

I would like you to comment on how well you think I met the requirements with the exception of 1) loading the african dinosaur data and 2) exporting the data to JSON. (The reason I have not completed these is because I am pretty sure I can do the first one, and I need to do further homework before I can do the second one).

I had a tough time figuring out how to make the Dinodex#filters method recursive, until I realized I can just update what the local variable selection points to. Once the right dinosaurs have been selected, a new Dinodex object is returned, so that chaining is possible.

OK, the code is this:

I would like you to comment on how well you think I met the requirements with the exception of

  1. loading the African dinosaur data
  2. exporting the data to JSON (the reason I have not completed these is because I am pretty sure I can do the first one, and I need to do further homework before I can do the second one)

I had a tough time figuring out how to make the Dinodex#filters method recursive, until I realized I can just update what the local variable selection points to. Once the right dinosaurs have been selected, a new Dinodex object is returned, so that chaining is possible.

Source Link
Steven L.
  • 425
  • 2
  • 8
Loading