Skip to content

Instantly share code, notes, and snippets.

@nmcolome
Created April 6, 2017 16:13
Show Gist options
  • Save nmcolome/9c2ef80717f42c6e3eb01bce4ec9d137 to your computer and use it in GitHub Desktop.
Save nmcolome/9c2ef80717f42c6e3eb01bce4ec9d137 to your computer and use it in GitHub Desktop.
diagnostic_week_4_Natalia_Colomé

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.

You use Classes when you want to have instances of that object, Modules don't generate instances. Difference = instances

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.

class Pizza
  include Doughy

  def tasty?
    true
  end
end
module Doughy
  
  def has_carbs?
    true
  end
end
3. What are two benefits modules provide us in Ruby? Elaborate on each.
  • It helps us not to repeat ourselves (mixin) --- if I have several classes that use the same/similar methods, I can use a module and move those methods there and that way I can avoid writing the same code over and over again.
  • It helps us with whitespacing --- when we have methods that are named the same but do different things depending on the class they're in, we can use modules to give them 'context' so that way the program can differentiate which one to use.
4. What values in Ruby evaluate as "falsy"?

nil

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

3 == 3.0

nil.nil?

"HELLO" == "hello".upcase

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 -v 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 push origin iteration-1
    git checkout master
    git merge master 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
#(assuming we are on file_two)

require './lib/file_one.rb'
require_relative 'file_one.rb'
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.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].split(//) #change1
    last_four_digits = last_four_digits.map { |num| num.to_i } #change2
  end
end

Encryptor.new.date_offset
@nmcolome
Copy link
Author

nmcolome commented Apr 10, 2017

  1. Also False

  2. 0, string, class, everything else

6.2 Create a new branch
git branch iteration-2 || Git checkout -b [branch-name] (not -v)
6.4 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

  1. when you're seeing words repeating, we can refactor them into methods
class Encryptor
  attr_reader :date

  def initialize (date = Time.now.strftime("%d%m%y").to_i)
    @date = date
  end

  def date_offset
    split_date.map { |num| num.to_i }
  end

  def split_date
    date_squared.to_s[-4..-1].chars
  end

  def date_squared
    date ** 2
  end
end

Encryptor.new.date_offset

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