Skip to content

Instantly share code, notes, and snippets.

@kheppenstall
Forked from mikedao/w4.md
Last active October 27, 2016 15:25
Show Gist options
  • Save kheppenstall/eec801e0579593d3855b3afd5b8f7d35 to your computer and use it in GitHub Desktop.
Save kheppenstall/eec801e0579593d3855b3afd5b8f7d35 to your computer and use it in GitHub Desktop.

Module 1 Week 4 Diagnostic

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

For these questions, write a short description or 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. Give one difference between Modules and Classes.

Classes can be instantiated into objects. Modules can name space classes or provide a set of methods for a class but can never be instantiated into an objects with attributes like instance variables.

2. Defining Modules

First, create a module Doughy which defines a method has_carbs? that always returns true.

module Doughy
  def self.has_carbs
    true
  end
end

Then, given the following Pizza class:

class Pizza
  def tasty?
    true
  end
end

Update Pizza to use your new Doughy module to gain the defined has_carbs? behavior.

class Pizza
  def tasty?
    Doughy.has_carbs
  end
end

3. More Modules

Given the following class and Module:

module Nonprofit
  def tax_exempt?
    true
  end
end

class Turing
end

Write code that enables the Turing class to use the Nonprofit module to allow the following behavior:

Turing.tax_exempt?
=> true
module Nonprofit
  def tax_exempt?
    true
  end
end

class Turing
  include Nonprofit
end

4. List 3 HTTP Verbs
GET
POST
PUT

5. HTTP Parsing

Given the following HTTP Request:

POST /students?name=horace HTTP/1.1
Host: 127.0.0.1:9292
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

Identify the:

1. HTTP Verb: POST
2. Request Path: /students
3. Query Parameters: key = name, value = horace

Additionally, give the full request URL:

http:///students?name=horace

6. Git and Branches

Give a git command to accomplish each of the following:

  1. Switch to an existing branch iteration-1
  2. Create a new branch iteration-2
  3. Push a branch iteration-2 to a remote origin
  4. Merge a branch iteration-1 into a branch master (assume you are not on master to begin with)
1. Git branch iteration-1, git checkout iteration-1
2. Git branch iteration-2
3. Git push origin iteration-2
4. Git checkout master, git merge iteration-1

7. Load Paths and Requires

Given a project with the following directory structure:

. <you are here>
├── lib
│  │── file_one.rb
│  └── file_two.rb

Give 2 ways that we could require file_one from file_two.

require_relative file_one
require ./lib/file_one

8. Refactoring

Given the following snippet of code:

class Encryptor
  def date_offset
    date = Time.now.strftime("%d%m%y").to_i
    date_squared = date ** 2
    last_four_digits = date_squared.to_s[-4..-1]
    [last_four_digits[-4].to_i,
     last_four_digits[-3].to_i,
	 last_four_digits[-2].to_i,
	 last_four_digits[-1].to_i]
  end
end

Encryptor.new.date_offset

Show 2 refactorings you might make to improve the design of this code.


Refactor 1

class Encryptor
  def date_offset
    date = Time.now.strftime("%d%m%y").to_i
    date_squared = date ** 2
    digits = date_squared.to_s[-4..-1].chars
    digits.map {|digit| digit.to_i }
  end
end

Encryptor.new.date_offset

Refactor 2

class Encryptor

  def date_offset
    date = Time.now.strftime("%d%m%y").to_i
    date_squared = date ** 2
    last_four_digits(date_squared)
   end
   
   def last_four_digits(date_squared)
     digits = date_squared.to_s[-4..-1]
     [digits[-4].to_i, digits[-3].to_i, digits[-2].to_i, digits[-1].to_i]
   end
  
end

Encryptor.new.date_offset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment