Skip to content

Instantly share code, notes, and snippets.

@VictoriaVasys
Last active January 30, 2017 17:13
Show Gist options
  • Save VictoriaVasys/541205139a6e39555692164b794bf4c0 to your computer and use it in GitHub Desktop.
Save VictoriaVasys/541205139a6e39555692164b794bf4c0 to your computer and use it in GitHub Desktop.
A diagnostic of what I've learned from week 1, from memory

Floats and Integers

What’s the difference between a float and integer?

  • A float is a numeric with a decimal, an integer is a whole-number numeric

What’s are the similarities and differences between BigNum and FixNum?

  • They are both integers, but BigNum is a very large number (> 18 or 19 digits long, depending on the binary equivalent), while FixNum is smaller.
NEITHER CAN ACCOMODATE DECIMALS

What will 4.0 / 2 return?

  • 2.0

What will 1.5.to_i.to_f return?

  • 1.0

What part of this expression ("hi" * 5?) is “syntactic sugar”? What does that mean?

  • The *; it means that it is an abbreviated representation of a method (in this case, it means multiply; it's abbreviated from .*)
HELPS MAKE CODE MORE READABLE

What will 10 % 3 return?

  • 1

What will 5 == 10/2 return?

  • true

How can you write 5 to the 2nd power?

  • 5 ** 2

Strings

How can you grab the substring “ell” from “hello”?

  • "hello"[1..3]

Give an example of string concatenation and string interpolation.

  • Concatenation: "hello" + "there"
  • Interpolation: "hello #{2+4}th guest"

Give examples of three variable names: a “valid name” (it will work) that goes against Ruby style, an “invalid” name (it will trigger an error), and a valid and stylistically conventional name.

  • Valid: VarName
  • Invalid: 3_buckets
  • Valid & Stylistic: var_name

Arrays

How do you add to an array?

  • With the shovel operator, #push, or #unshift (respectively: [3, 2] << 1; [3, 2].push(1); [2, 1].unshift(1))

What will [1,2,3].shuffle do?

  • It'll give you a new array with the elements 1, 2, & 3 in randomized order

What do shift and unshift do?

  • #shift removes the first element of an array, unshift(<element>) inserts <element> to become the 0th index of an array

Name 3 ways to retrieve 4 from this array: [4,3,5]

arr = [4,3,5]
arr[0]
arr.first
arr.shift

How would you print each word from this list (["hello", "goodbye", "cactus"]) to terminal within this output:

The word is "hello".
The word is "goodbye".
The word is "cactus".
(["hello", "goodbye", "cactus"]).each {|word| puts "The word is #{word}"}
SHOULD BE:
{|word| puts "The word is \"#{word}\""}
OR
{|word| puts "The word is #{word.inspect}"

Flow control

What’s the difference between a while and until loop?

  • They require opposite conditional operators to exit the loop; while requires a condition that keeps the loop going (or else it will exit) & until requires a condition that will force the loop to exit when evaluated to true

How do you escape a pry in the middle of a loop?

  • three bangs; !!!
OR:
exit, next?

Enumerables (.each)

What is the purpose of an enumerable?

  • An enumerable loops through every element in a collection & does whatever the code tells it to do to that element

What are the two different ways that you can represent a block?

  • with curly braces or do/end:
arr.each {|thing| thing * 2} 

or

arr.each do |thing|
  thing * 2
end

Given this array ["Beth", "Lauren", "Ilana"]. How would you create a new array of just the first initials of each name?

names = ["Beth", "Lauren", "Ilana"]
initials = []
names.each do |name|
  initials << name[0]
end
BETTER TO DO MAP

Classes and instance methods

When you’re defining a class, how do you store attributes?

  • with an @ symbol; @attribute

What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do?

  • attr_readers abbreviate the method to be able to call & view an attribute from an instance; attr_writers abbreviate the method to be able to write to (change) an attribute without being able to call it from an instance; attr_accessors combine the attr_reader & attr_writer functions
ATTR_READER REPLACES GETTER METHOD, RETRIEVES AN INSTANCE VAR
ATTR_WRITER REPLACES SETTER METHOD, LETS YOU (SET OR) RESET AN INSTANCE VAR
class Persons
  attr_reader :age
  attr_writer :gender
  attr_accessor :job
  
  def initialize(age, gender, job)
    @age = age
    @gender = gender
    @job = job
  end
  
  # attr_reader replaces this method:
  # def age
  #   @age
  # end
  
  # attr_writer replaces this method:
  # def age= (new_age)
  #   @age
  # end
  
  # attr_accessor replaces both of the previous 2 methods at once
  
end

What method corresponds with .new when you create a new instance of a class?

  • initialize

Methods, arguments and scopes

Describe the scope that methods create. Are variables created in that scope accessible elsewhere?

  • Methods create their own scope; variables created within that scope are not available in parent scopes but are available to some children scopes (not available to children methods but available to children blocks)

What does the method scope have access to?

  • The method scope has access to other methods on the same level as the method (if the method is on the global scope, it can access other methods on the global scope; thus if a method has code with an unassigned variable in it, it will first look for a local variable to retrieve the value and if it can't find one, it will then look for a method in the scope above)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment