Floats and Integers
What’s the difference between a float and integer?
- A float has a decimal point, while an integer is a whole number. Makes a difference with division.
What’s are the similarities and differences between BigNum and FixNum?
- BigNum and FixNum are both types of integers. A FixNum is a "small" number, while a BigNum is a "big" one (very big). Something to do with hexidecimal digits, I think.
What will 4.0 / 2 return?
- 2.0
What will 1.5.to_i.to_f return?
- 1.0 (to interger, truncates decimal, then back to float)
What part of this expression ("hi" * 5?) is “syntactic sugar”? What does that mean?
- Syntatic sugar are shortcuts in expression. The * is actually a method, you could call it
.*
What will 10 % 3 return?
- 3 returns 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”?
- Call the index of the substring hello.[2..3], among other methods. **
"hello"[1..3]
or counting from the right [-4..-4] or using an inclusive range (...)"
Give an example of string concatenation and string interpolation.
- string concatenation is adding strings, e.g.
puts "he" + "llo" => "hello"
- string interpolation only works in double quotes, inserts strings (as well as evaluates in the interpolated string), e.g.
x = "hi" puts "#{x} there"
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, unstyled name
workTime
- Invalid name,
99bottles
- Valid and stylish name,
time_count
Arrays
How do you add to an array?
- array.push appends the item onto the array, and shovel << does the same thing. Shift puts it into the first positions, into 0th index. ** unshift adds it to the front, not shift ***
What will [1,2,3].shuffle do?
- return an array with the three elements randomly arranged.
What do shift and unshift do?
- shift adds and item to the front of the array, unshift pops the item off the front of the array and returns it ** got this backwards, unshift adds, shift removes **
Name 3 ways to retrieve 4 from this array: [4,3,5]
- array[1], array.first, array.unshift [-3], .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".
words.each { |word| puts 'The world is "#{word}".' }```
Flow control
What’s the difference between a while and until loop?
- A while loop continues while the condition it has is true (stops when it becomes false).
- An until loop continues until its condition becomes true. (waiting for a true condition)
How do you escape a pry in the middle of a loop?
- you can loop at the next loop with
next
, or exit pry entirely with!!!
** or exit**
Enumerables (.each)
What is the purpose of an enumerable?
- enumberables allow you to iterate over a collection. ** "move through and interact w collection"**
What are the two different ways that you can represent a block?
- do/end for multiline or {|var|} for single line.
eg.
array.each do |var| # do something end
eg.array.each { |var| # do something }
Given this array ["Beth", "Lauren", "Ilana"]. How would you create a new array of just the first initials of each name?
array.each {|name| puts name[1] }
** array.map {|name| name[0]} **
Classes and instance methods
When you’re defining a class, how do you store attributes?
- attributes are stored as instance variables (begin with @) in the initialize method
What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do?
- attr_readers are getter methods, shorthand for defining a method that can be read later in the class. ** allows you to run a method on an instance of your class, retrieves and insance variabel**
- attr_writers are setter methods, allowing you to change a given attribute later (e.g. change temp of fridge) **allows to reset instance variable **
- attr_accessors do both roles
What method corresponds with .new when you create a new instance of a class?
- the initialize method corresponds with .new, when you create a new instance (a baby!) of a class
Methods, arguments and scopes
Describe the scope that methods create. Are variables created in that scope accessible elsewhere?
- methods create a local scope, and variables created within it are accessible only to the method locally. they are not accessible elsewhere, and the method can't access global variables. The method itself exists globally.
What does the method scope have access to?
- method scope has access to other methods, they are like sibilings. the method first looks for a local variable and then looks for a method by that name. ** if we pass in arguement, it can access that, also called a local variable. also instance variables in the class it has access to**
Took me 25 minutes, from 10:21 - 10:46