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).
Modules mainly collect methods that classes can use.
First, create a module Doughy
which defines a method
has_carbs?
that always returns true
.
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.
module Doughy
def has_carbs?
true
end
end
class Pizza
include Doughy
def tasty?
true
end
end
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:
class Turing
include Nonprofit
end
Turing.tax_exempt?
=> true
GET
POST
???
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:
- HTTP Verb
POST
- Request Path
/students
- Query Parameters
name = horace
Additionally, give the full request URL:
http://127.0.0.1:9292/students?name=horace
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 --b iteration-2
- Push a branch
iteration-2
to a remoteorigin
push origin iteration-2
- Merge a branch
iteration-1
into a branchmaster
(assume you are not onmaster
merge iteration-1 master
to begin with)
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 '.lib/file_one'
require_relative 'file_two'
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.
- pull last_four_digits out of this method - make a new method
class Encryptor
def date_offset
date = Time.now.strftime("%d%m%y").to_i
date_squared = date ** 2
get_last_four_digits(date_squared)
end
def get_last_four_digits(date_squared)
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
1. simplify get_last_four_digits with an enumerable ```ruby class Encryptor def date_offset date = Time.now.strftime("%d%m%y").to_i date_squared = date ** 2 get_last_four_digits(date_squared) end
def get_last_four_digits(date_squared) last_four_digits = date_squared.to_s[-4..-1] array = last_four_digits.map do |number| array << number.to_i end return array end end
Encryptor.new.date_offset