Last active
August 29, 2015 14:24
-
-
Save dummied/0c4a3ae8b92575f1ef78 to your computer and use it in GitHub Desktop.
Week 2, Day 1 morning in-class code
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 Enumerables | |
def self.enumerable_to_test | |
CSV.foreach('planet_express_logs.csv', headers: true) | |
end | |
def self.hard_earth_records | |
our_earths = [] | |
self.enumerable_to_test.each do |row| | |
if row["Destination"] == "Earth" | |
our_earths << row | |
end | |
end | |
our_earths | |
end | |
def self.easy_earth_records | |
self.enumerable_to_test.select do |row| | |
row["Destination"] == "Earth" | |
end | |
end | |
def self.hard_collect_destinations | |
our_destinations = [] | |
self.enumerable_to_test.each do |row| | |
our_destinations << row["Destination"] | |
end | |
our_destinations | |
end | |
def self.easy_collect_destinations | |
self.enumerable_to_test.collect{|row| row["Destination"]} | |
end | |
def self.hard_sum_earnings | |
sum = 0 | |
self.enumerable_to_test.each do |row| | |
sum = sum + row[" Money we made"].to_i | |
end | |
sum | |
end | |
def self.easy_sum_earnings | |
self.enumerable_to_test.inject(0) do |sum, row| | |
sum + row[" Money we made"].to_i | |
end | |
end | |
# {"Earth" => [row1, row3, row4], | |
# "Moon" => [row2, row5], | |
# } | |
def self.group_by_destination | |
destinations_hash = {} | |
self.enumerable_to_test.each do |row| | |
if destinations_hash[row["Destination"]].nil? | |
destinations_hash[row["Destination"]] = [] | |
end | |
destinations_hash[row["Destination"]] << row | |
end | |
destinations_hash | |
end | |
def self.easy_group_by_destination | |
self.enumerable_to_test.group_by{|row| row["Destination"]} | |
end | |
def self.hard_nonearth_records | |
our_nonearths = [] | |
self.enumerable_to_test.each do |row| | |
unless row["Destination"] == "Earth" | |
our_nonearths << row | |
end | |
end | |
our_nonearths | |
end | |
def self.easy_nonearth_records | |
self.enumerable_to_test.reject do |row| | |
row["Destination"] == "Earth" | |
end | |
end | |
def self.collect_nonsense | |
self.enumerable_to_test.collect do |row| | |
if row["Destination"] == "Earth" | |
"Homebase" | |
elsif row["Destination"] == "Mars" | |
"Red Planet" | |
elsif row["Destination"] == "Moon" | |
"Cheeseland" | |
else | |
"Middle of nowhere" | |
end | |
end | |
end | |
def self.say_hi | |
"Howdy, ya'll" | |
end | |
def self.moon_trips | |
self.enumerable_to_test.select do |row| | |
row["Destination"] == "Moon" | |
end | |
end | |
end |
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 'date' | |
class Person | |
attr_accessor :hair_color, | |
:eye_color, | |
:disposition, | |
:name, | |
:time_of_death | |
def initialize(hash) | |
@hair_color = hash[:hair_color] | |
@eye_color = hash[:eye_color] | |
@disposition = hash[:disposition] | |
@name = hash[:name] | |
end | |
def say_hi | |
"Howdy!" | |
end | |
def walk(steps=60) | |
"I walked #{steps} steps" | |
end | |
def say_name | |
"Hi, my name is #{name}" | |
end | |
def die | |
self.time_of_death = Time.now | |
end | |
def deceased? | |
# time_of_death = DateTime.parse("July 17, 2015 09:32:45").to_time | |
time_of_death < Time.now | |
end | |
def arise! | |
self.time_of_death = nil | |
end | |
def alive? | |
time_of_death.nil? || time_of_death > Time.now | |
end | |
def self.gather | |
10.times.collect do |num| | |
Person.new({name: "Person #{num + 1}"}) | |
end | |
end | |
end |
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 'minitest/autorun' | |
require './person' | |
class PersonTest < MiniTest::Test | |
def test_new_person | |
attributes = { | |
eye_color: "blue", | |
hair_color: "blonde", | |
disposition: "hangry" | |
} | |
person = Person.new(attributes) | |
assert person.eye_color == "blue" | |
assert person.hair_color == "blonde" | |
assert person.disposition == "hangry" | |
end | |
def test_say_hi | |
person = Person.new({eye_color: "blue"}) | |
assert person.say_hi == "Howdy!" | |
end | |
def test_walk | |
person = Person.new({eye_color: "blue"}) | |
assert person.walk(10) == "I walked 10 steps" | |
assert person.walk == "I walked 60 steps" | |
end | |
def test_person_has_name | |
person = Person.new({name: "Chuck"}) | |
assert person.name == "Chuck" | |
end | |
def test_say_name | |
person = Person.new({name: "Charles"}) | |
assert person.say_name == "Hi, my name is Charles" | |
end | |
def test_die | |
person = Person.new({name: "Death"}) | |
person.die | |
assert person.deceased? | |
end | |
def test_rebirth | |
person = Person.new({name: "George"}) | |
person.die | |
assert person.deceased? | |
person.arise! | |
assert person.alive? | |
end | |
def test_gather | |
people = Person.gather | |
assert people.length == 10 | |
assert people.first.name == "Person 1" | |
end | |
end |
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 'minitest/autorun' | |
require './enumerables' | |
class EnumerablesTest < MiniTest::Test | |
def test_selects | |
assert Enumerables.hard_earth_records == Enumerables.easy_earth_records | |
end | |
def test_collects | |
assert Enumerables.hard_collect_destinations == | |
Enumerables.easy_collect_destinations | |
end | |
def test_injects | |
assert Enumerables.hard_sum_earnings == Enumerables.easy_sum_earnings | |
end | |
def test_groups | |
assert Enumerables.group_by_destination == | |
Enumerables.easy_group_by_destination | |
end | |
def test_rejects | |
assert Enumerables.hard_nonearth_records == Enumerables.easy_nonearth_records | |
end | |
def test_say_hi | |
assert Enumerables.say_hi == "Howdy, ya'll" | |
end | |
def test_moon_trips | |
assert Enumerables.moon_trips.length == 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment