Skip to content

Instantly share code, notes, and snippets.

@JoelLindow
Last active March 20, 2017 16:46
Show Gist options
  • Save JoelLindow/abfe5c3a26ccac65fc17411c7ea83154 to your computer and use it in GitHub Desktop.
Save JoelLindow/abfe5c3a26ccac65fc17411c7ea83154 to your computer and use it in GitHub Desktop.
# Week 1 Diagnostic
DO NOT GOOGLE ANSWERS! ANSWER FROM YOUR OWN KNOWLEDGE!
## Floats and Integers
* What's the difference between a float and integer?
- An integer represents a whole number. A float represents a decimal number.
* What's are the similarities and differences between BigNum and FixNum?
- I've never heard of BigNum. Fixnum is a class that holds a number or something like that, but I'm not sure!
* What will `4.0 / 2` return?
- 2.0, becasue you're dividing a float so it will return a float.
* What will `1.5.to_i.to_f` return?
- 2.0 becasue you're converting it from a float (which rounds it to a whole number(integer) and then back to a float (which will hold a decimal place).
* What part of this expression (`"hi" * 5`?) is "syntactic sugar"? What does that mean?
- Possibly the ? symbol. Syntactic sugar is little things you can do in Ruby to access functions easier.
* 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”?
-
* Give an example of string concatenation and string interpolation.
- last_name = "Lebowski"
- puts "You are Mr. #{last_name}" <---- String Interpolation
- puts "You are Mr. " + last_name <---- String Concatenation
* 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.
- array <--- valid name that goes against rubys style, by naming a variable after an actual Ruby function
- c#music <--- returns an error. Can't use that symbol in there!
- last_name <--- valid and stylistically conventional name
## Arrays
* How do you add to an array?
- You can shovel stuff into an array.
[1, 2, 3].push(4)
* What will `[1,2,3].shuffle` do?
- Randomly shuffle the array. However, it will not mutate the array
* What do `shift` and `unshift` do?
- Shift returns the value of the first item in an array, and shifts that item out of the array (mutates the array).
- Unshift pushes a new element into an array at the 0 index position. [1, 2, 3].unshift(0) = [0, 1, 2, 3]
* Name 3 ways to retrieve `4` from this array: `[4,3,5]`
- array[0]
- array.shift <--- dangerous!!!!
- 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.each do |one|
- puts "The word is \"#{one}\""
- end
OR
- list.each do |word|
- puts "The word is \"#{one}\"". <----escape quotes!!!!! WOOT!
- end
## Flow control
* What's the difference between a `while` and `until` loop?
- A while loop continues while conditions are being met. (While breaks when condition is false)
- An until loop continues until conditions are being met. (Until breaks when condition is true)
* How do you escape a pry in the middle of a loop?
- !!!
- ctr - c
## Enumerables (`.each`)
* What is the purpose of an enumerable?
- To be able to iterate through an array
* What are the two different ways that you can represent a block?
- do / end
- { }
* Given this array `["Beth", "Lauren", "Ilana"]`. How would you create a new array of just the first initials of each name?
- initials = names.map do |initial|
initial[0]
end
- I don't feel we learned this in class. We need to talk about the .map function more, as it's a very powerful tool a lot of us don't understand. Also, until just now... I had no idea you could assign an operation to a variable. This part took me playing in IRB for a long time and asking a lot of questions to figure out, becasue I refused to leave this unanswered for my own benefit. I know we were supposed to do this whole diagnostic rom memory, but this part would have been impossible for me.
## Classes and instance methods
* When you're defining a class, how do you store _attributes_?
- using the initialize method (and adding instance variables)
* What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do?
- attr_reader = user can only read (gives back value)
- attr_writers = user can only write
- attr_writers - user can read and write
* What method corresponds with `.new` when you create a new instance of a class?
- initialize method
## Methods, arguments and scopes
* Describe the scope that methods create. Are variables created in that scope accessible elsewhere?
- The scope that methods create only reach from the definition of the method to the "end" statement.
(Local scope in which variables (local variables) are not accessable elsewhere)
* What does the method scope have access to?
- arguements passed into the method, or actual operations being performed within the method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment