Skip to content

Instantly share code, notes, and snippets.

@dannyradden
Last active January 30, 2017 17:07
Show Gist options
  • Save dannyradden/d50f8a7e8095bf456aa3a22345d86ddd to your computer and use it in GitHub Desktop.
Save dannyradden/d50f8a7e8095bf456aa3a22345d86ddd to your computer and use it in GitHub Desktop.
Create a gist and answer the following questions.
Time to complete: 30 minutes
Floats and Integers
What’s the difference between a float and integer?
*A float is a decimal number. An integer is just a whole number.
What’s are the similarities and differences between BigNum and FixNum?
*Both are decimal numbers. If a number is too big to fit into 64 bits of memory it is stored as a BigNum otherwise it is a fixnum.
**Both are Integers
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 question mark is syntactic sugar. Syntactic sugar is basically condensed code.
** The asterisk is the syntactic suger.
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".cantremembermethodname(2..4)
** "hello"[1..3]
Give an example of string concatenation and string interpolation.
* Concatenation: "he" + "llo"
* Interpolation: variable = "ll"
* "he + #{variable} + o"
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 name against style: classRoster
* invalid name : VARIABLE!!
* valid and good : class_roster
Arrays
How do you add to an array?
* use << to shovel into the end of an array
What will [1,2,3].shuffle do?
* it will randomly organize the 3 numbers in the array
What do shift and unshift do?
* shift removes the first element in an array and returns that element. unshift adds an element to the beginning of an array and returns that element.
Name 3 ways to retrieve 4 from this array: [4,3,5]
* array[0], array.shift (though it will change the array), array.first
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".
* array = ["hello", "goodbye", "cactus"]
* prints "The word is "#{array[0]}"."
* prints "The word is "#{array[1]}"."
* prints "The word is "#{array[2]}"."
Flow control
What’s the difference between a while and until loop?
* while runs the loop when the condition is true
* until runs the loop when the condition is false
How do you escape a pry in the middle of a loop?
* !!!
Enumerables (.each)
What is the purpose of an enumerable?
* To do something for each element of an array.
What are the two different ways that you can represent a block?
* with .each or with a method
Given this array ["Beth", "Lauren", "Ilana"]. How would you create a new array of just the first initials of each name?
* new_array = []
* ["Beth", "Lauren", "Ilana"].each do |x|
* new_array << x.first
* end
** array.map { |name| name[0] }
Classes and instance methods
When you’re defining a class, how do you store attributes?
* With instance variables
What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do?
* att_readers allow you to call instance variables from outside of the class.
* att_writers allow you to write to instance variables from outside of the class.
* att_accessors allow you to do both.
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?
What does the method scope have access to?
* local variables created inside a method cannot be accessed from elsewhere. The method cannot access global variables.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment