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).
- One difference between a module and a class is that modules can be used as mixins (mix in the methods contained within them). A module 4 student I paired with described them as a "toolbox" in that sense.
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
end
- Namespacing - related classes can be kept within one module
- Mixin - keep a set of tools (methods) in the module that you can "mix in" to other classes. This allows for more DRY programming, as you can share the same tools between different classes, without having to define them more than once.
- Only zero (0) and nil.
- Any integer, any string, the reserved word
true
- get, post, and ?
- HTTP Verb -
POST
- Request Path -
/students?name=horace
- Query Parameters -
name=horace
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
- Switch to an existing branch iteration-1
git checkout iteration-1
- Create a new branch iteration-2
git checkout -b iteration-2
this creates it and switches to it - I don't know the command to just create it, maybegit -b iteration-2
- Push a branch iteration-2 to a remote origin
git push origin iteration-2
if it does not exist upstream orgit push
if you are on the branch and it already exists
- Merge a branch iteration-1 into a branch master (assume you are not on master to begin with)
git merge master
(guessing on this one, only done it from github)
Given a project with the following directory structure, give 2 ways that we could require file_one from file_two.
- Put one of the following inside your file_two file
'require' './lib/file_one'
'require_relative' 'file_one'
. <you are here>
├── lib
│ │── file_one.rb
│ └── file_two.rb
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_squared = (Time.now.strftime("%d%m%y").to_i) ** 2 # here is one
last_four_digits = date_squared.to_s[-4..-1].split('').map { |n| n.to_i } # here is another
end
end
Encryptor.new.date_offset