Create a gist and answer the following questions.
- What's the difference between a float and integer? A float has decimals (4.56) and an integer is a whole number (4)
- What's are the similarities and differences between BigNum and FixNum? FixNums become BigNums when they are too big.
- 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? In the expression, syntactic sugar is "". It means it's an easier option to understand for a human being (instead of using .times or .each we use "" which for us means times so its easier for us to understand but for the computer it makes no difference and does exactly the same thing). - What will
10 % 3
return? 1 - What will
5 == 10/2
return? true - How can you write 5 to the 2nd power? 5 ** 2
- How can you grab the substring “ell” from “hello”? "hello"[1..3]
- Give an example of string concatenation and string interpolation. concatenation = "hello" + " world" #returns "hello world" interpolation = variable = "world" puts "hello #{variable} #returns "hello world"
- 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 against style= variableOne
invalid name = 1variable
valid and with style= variable_1
- How do you add to an array? with .push or <<
- What will
[1,2,3].shuffle
do? It will shuffle them randomly (put them in different order each time) but does not change the actual array, it will only output them that way that time. - What do
shift
andunshift
do? they add or delete an element in the beginning of an array. - Name 3 ways to retrieve
4
from this array:[4,3,5]
1st way = array[0]
2nd way = array[-3]
3rd way = array.first
- How would you print each word from this list (
["hello", "goodbye", "cactus"]
) to terminal within this output: list = ["hello", "goodbye", "cactus"]
list.each do |word| puts "The word is "#{word}"." end
The word is "hello".
The word is "goodbye".
The word is "cactus".
- What's the difference between a
while
anduntil
loop? while keeps running "while" the condition keeps being true (if it doesn't change), until runs "until" the condition turns true (if it changes). - How do you escape a pry in the middle of a loop? ctrl + c
- What is the purpose of an enumerable? enumerables are condensed ways to do .each for a certain purpose
- What are the two different ways that you can represent a block? with "do" and "end"; or with "{" "}"
- Given this array
["Beth", "Lauren", "Ilana"]
. How would you create a new array of just the first initials of each name? new_array = array.map do |name| name.to_s[0] end
- When you're defining a class, how do you store attributes? with @attribute = attribute in the initialize
- What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do? the difference is what they allow: attr_readers, allows us to only read the variable (allows output, we cannot change it) attr_writers, allows us to only change the variable, but whe won't be able to read (output) it. attr_accessors, allows both reading and changing a variable.
- What method corresponds with
.new
when you create a new instance of a class? def initialize end
- Describe the scope that methods create. Are variables created in that scope accessible elsewhere? Methods create a scope with their own variables that we define in order to run the method. If they don't find those variables they will go up to the instance variables and use those. The variables created within the methods scope can only be accessed inside the method.
- What does the method scope have access to? to the methods variables
Floats and Integers - comments
*Bignum anything greater 2 **62 - 62 bit processing; If a number is more than 64 digits. Integers include both bignum and fixnum
*Syntactic sugar = it’s a method under the hood. We will use so much in ruby that there is a short version to call it through operators
Array - comments
*Shuffle = it will reassign the indices of the array
*"The word is "#{variable}"." -- to print "" within ""
Enumerables - comments
*Name.chars.first
Classes - comments
Methods, scopes - comments
*first question:
Instance: available through the entire class
local var: is only inside the method, you can only access it from inside a method
*second question: local variables and sibling methods
General comments: be more specific when answering (e.g .shift and .unshift - I wasn't clear enough)