Last active
February 17, 2017 17:20
-
-
Save lukyans/28432e4af7aa72252ba14b791c5313a9 to your computer and use it in GitHub Desktop.
Module 1 Week 4 Diagnostic
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
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. | |
``` | |
Modules can be initializd. | |
Modules have mixins. | |
``` | |
2. Defining Modules | |
First, create a module Doughy which defines a method has_carbs? that always returns true. Then, given the following Pizza class, update Pizza to use your new Doughy module to gain the defined has_carbs? behavior. | |
``` | |
module Doughy | |
def has_carbs? | |
true | |
end | |
end | |
class Pizza | |
include Doughy | |
def tasty? | |
true | |
end | |
end | |
Pizza.new.has_carbs? | |
``` | |
3. What are two benefits modules provide us in Ruby? Elaborate on each. | |
``` | |
module has namespacing and mixins | |
1.When a module is used as a container for objects, it’s called a namespace | |
2.Mixins allow you to share functionality between classes. if a class mixes in a module, that module's instance methods become available as if they had been defined in the class. | |
``` | |
4. What values in Ruby evaluate as “falsy”? | |
``` | |
nil | |
false | |
``` | |
5. Give 3 examples of “truthy” values in Ruby. | |
``` | |
true | |
"hello" | |
0 | |
``` | |
6. List 3 HTTP Verbs | |
``` | |
GET | |
POST | |
PUT | |
``` | |
7. HTTP Parsing: given the following HTTP Request, identify the following: | |
HTTP Verb | |
Request Path | |
Query Parameters | |
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 | |
What is the full request URL? | |
``` | |
http://127.0.0.1:9292/students?name=horace | |
``` | |
8. Git and Branches: give a git command to accomplish each of the following: | |
Switch to an existing branch iteration-1 | |
git checkout iteration-1 | |
Create a new branch iteration-2 | |
git checkout -b iteration-2 | |
Push a branch iteration-2 to a remote origin | |
git push origin branchname | |
Merge a branch iteration-1 into a branch master (assume you are not on master to begin with) | |
git merge iteration-1 master | |
9. Load Paths and Requires | |
Given a project with the following directory structure, give 2 ways that we could require file_one from file_two. | |
. <you are here> | |
├── lib | |
│ │── file_one.rb | |
│ └── file_two.rb | |
``` | |
require "./lib/file_one" | |
require_relative "file_one" | |
``` | |
10. Refactoring: given the following snippet of code, show 2 refactorings you might make to improve the design of this 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 | |
``` | |
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.split("").map { |s| s.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