Skip to content

Instantly share code, notes, and snippets.

@JoelLindow
Last active April 10, 2017 16:32
Show Gist options
  • Save JoelLindow/78078292c95967f5ec7d2f06e1098f45 to your computer and use it in GitHub Desktop.
Save JoelLindow/78078292c95967f5ec7d2f06e1098f45 to your computer and use it in GitHub Desktop.
week_4_diagnostic_1703-BE

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.
  • Answer 1: Modules are not instantiated, while classes can be.
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.
  • Answer: Absolutely no idea how to do this. Don't even know where to start.
module Doughy
  class Pizza
    include Doughy
    def tasty?
      true
    end
  end

  def has_carbs?
    true
  end

  class ChicagoStyle < Pizza
    def pan?
      true
    end

    def tasty?
      false
    end
  end
end

pizza = Doughy::ChicagoStyle.new
puts pizza.pan?
puts pizza.tasty?
3. What are two benefits modules provide us in Ruby? Elaborate on each.
  • Answer_1: The can clean up our classes
  • Answer_2: Modules allow "mix-ins"
4. What values in Ruby evaluate as "falsy"?
  • Answer_1: nil
  • Answer_2: False
5. Give 3 examples of "truthy" values in Ruby.
  • Answer_1: "Banana"
  • Answer_2: []
  • Answer_3: {}
6. 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 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
7. 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_relative 'file_1'
  • require './lib/file_1'
8. 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)
  end
  
  def last_four_digits(date_squared)
  date = date_squared.to_s[-4..-1].chars
  date.map! { |num|
    num.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