Skip to content

Instantly share code, notes, and snippets.

@VictoriaVasys
Last active February 21, 2017 04:35
Show Gist options
  • Save VictoriaVasys/654f10064fb0b3b138fbce6e8bb9e49f to your computer and use it in GitHub Desktop.
Save VictoriaVasys/654f10064fb0b3b138fbce6e8bb9e49f to your computer and use it in GitHub Desktop.
1. Give one difference between Modules and Classes.

Modules contain static information, meaning they can be used as "mixins" where a class includes the module & thereby acts like the module's methods are its own.

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
3. What are two benefits modules provide us in Ruby? Elaborate on each.

Modules allow us to add namespacing & mixins to our classes. Namespacing lets us better modulate & organize classes of a similar type (you can create a class that is namespaced under a module that groups classes of similar functionality). Mixins allow us to group static information; that information can be "included" in any class that would like to use the module's functions as its own.

4. What values in Ruby evaluate as "falsy"?

Nil & False

5. Give 3 examples of "truthy" values in Ruby.

"0", 0, ""

6. List 3 HTTP Verbs

GET, POST, and HEAD

7. HTTP Parsing: given the following HTTP Request, identify the following:
  • HTTP Verb = "POST"
  • Request Path = "/students?name=horace"
  • Query Parameters = "name" (value is "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? = "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 branch iteration-2" or "git checkout -b iteration-2"
  • Push a branch iteration-2 to a remote origin = "git push origin iteration-2"
  • Merge a branch iteration-1 into a branch master (assume you are not on master to begin with) =
git checkout master
git merge iteration-1
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.

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_squared = Time.now.strftime("%d%m%y").to_i.send(:*, 2) # eliminates the need for date ** 2
    last_four_digits = date_squared.to_s[-4..-1].split("").map { |digit| digit.to_i} # eliminates the need for building an array & tranlating each to an integer
  end
end

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