Skip to content

Instantly share code, notes, and snippets.

@jesse-spevack
Forked from mikedao/week 2.markdown
Last active July 8, 2016 19:17
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 jesse-spevack/7105f25aaf28b1414314230d21031968 to your computer and use it in GitHub Desktop.
Save jesse-spevack/7105f25aaf28b1414314230d21031968 to your computer and use it in GitHub Desktop.

Module 1 Week 2 Diagnostic

This exercise is intended to help you assess your progress with the concepts and techniques we've covered during the week.

When complete, fill out this form.

For these questions, write a short snippet of code that meets the requirement. In cases where the question mentions a "given" data value, use the variable given to refer to it (instead of re-writing the information).

1. Define a class called PizzaOven which has a method cook_pizza which returns the string "mmm 'za"
class PizzaOven
  def cook_pizza
    "mmm 'za"
  end
end
  
2. Define a class called Student which is instantiated with a "name" value and which has a method name that returns this value
Class Student
  attr_accessor :name
end
3. Given an array of the numbers [1,2,3,4,5], find the sum of the doubles of all the numbers
numbers = [1,2,3,4,5]
double = numbers.map { |number| number * 2 }
sum = double.reduce(:+)
4. Give the command to create a new Git repository in a directory on your machine
git init
5. Testing and Pizza

Given a hypothetical Pizza class which has an instance method is_tasty? that always returns true, write a simple Minitest test that tests this behavior.

require 'minitest/autorun'
require 'minitest/pride'
require_relative '../lib/pizza'

class PizzaTest < Minitest::Test
  def test_pizza_knows_it_is_tasty
    pizza_1 = Pizza.new
    assert_equal true, pizza_w.is_tasty?
  end
end

Suppose the Pizza class also has a method style which randomly returns one of: "supreme", "mediterranean", or "cheese". Write a test that confirms that the returned pizza style is within this list.

require 'minitest/autorun'
require 'minitest/pride'
require_relative '../lib/pizza'

class PizzaTest < Minitest::Test
  def test_pizza_style_is_either_supreme_mediterranean_or_cheese
    pizza_1 = Pizza.new
    expected_styles = ["supreme", "mediterranean", "cheese"]
    actual_style = pizza_1.style
    assert expected_styles.include?(actual_style)
  end
end
6. Give the Git commands needed to stage and then commit a set of changes to a file
git add .
git ci -m "message"
7. More Objects

Define a Student class which, when created, has an attitude attribute. attitude should start out with the value "cheerful", and the Student class should provide a "reader" method that allows us to access the value of its attitude.

Additionally, add an assign_homework method to Student. When assigned_homework is invoked, if the student's attitude is "cheerful", it should become "dubious". If the value is currently "dubious" it should become "perturbed". If the value is currently "perturbed", it should become "dazed". Assigning homework to a "dazed" student has no effect.

class Student
  attr_reader :attitude

  def initialize
    @attitude = "cheerful"
    @emotions = ["cheerful", "dubious", "perturbed", "dazed"]
  end

  def assign_homework
    @attitude = student_dazed? ? "dazed" : escalate_emotional_state
  end

  def student_dazed?
    @attitude == "dazed"
  end

  def escalate_emotional_state
    @emotions[@emotions.index(@attitude)+1]
  end

end
8. Adding to Students

Building on the Student class from the previous example, update the assign_homework method to accept an argument. The argument will be a String containing a short description of the assignment. For example we might use it like this:

s = Student.new
s.assign_homework("Write a linked list")

Then, add an assignments method to Student. assignments should return a list of all the assignments that have been given, separated by a comma and a space. For example:

s = Student.new
s.attitude
=> "cheerful"
s.assign_homework("write a linked list")
s.attitude
=> "dubious"
s.assign_homework("write a BST")
s.attitude
=> "perturbed"
s.assignments
=> "write a linked list, write a BST"
class Student
  attr_reader :attitude,
              :emotions

  def initialize
    @attitude = "cheerful"
    @emotions = ["cheerful", "dubious", "perturbed", "dazed"]
    @assignments = []
  end

  def assign_homework(assignment)
    add_assignment(assignment)
    @attitude = student_dazed? ? "dazed" : escalate_emotional_state
    return
  end

  def student_dazed?
    @attitude == "dazed"
  end

  def escalate_emotional_state
    @emotions[@emotions.index(@attitude)+1]
  end

  def add_assignment(assignment)
    @assignments << assignment
  end

  def assignments
    @assignments.join(", ")
  end

end
9. Inheritance

Create a new class SurlyStudent which inherits from the Student class above. However, whenever you ask a SurlyStudent for their attitude, they always respond with "disgruntled"

  class SurlyStudent < Student
    def attitude
      "disgruntled"
    end
  end
10. Given an array of 3 Student instances, generate a new string of all of their assignments

For example:

s1 = Student.new
s2 = Student.new
s3 = Student.new

s1.assign_homework("linked list")
s1.assign_homework("sorting algos")

s2.assign_homework("write a c compiler")
s2.assign_homework("write a pacman game")

s3.assign_homework("headcount")
s3.assign_homework("sales engine")

students = [s1,s2,s3]

# YOUR CODE HERE

=> "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"
class Student
  @@class_assignments = []

  attr_reader :attitude,
              :emotions

  def initialize
    @attitude = "cheerful"
    @emotions = ["cheerful", "dubious", "perturbed", "dazed"]
    @assignments = []
  end

  def assign_homework(assignment)
    add_assignment(assignment)
    @attitude = student_dazed? ? "dazed" : escalate_emotional_state
    return
  end

  def student_dazed?
    @attitude == "dazed"
  end

  def escalate_emotional_state
    @emotions[@emotions.index(@attitude)+1]
  end

  def add_assignment(assignment)
    @assignments << assignment
    @@class_assignments << assignment
  end

  def assignments
    @assignments.join(", ")
  end

  def what_is_everyone_working_on
    @@class_assignments.join(", ")
  end

end

class SurlyStudent < Student
  def attitude
    "disgruntled"
  end
end

students[0].what_is_everyone_working_on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment