Skip to content

Instantly share code, notes, and snippets.

@markyv18
Last active January 30, 2017 17:07
Show Gist options
  • Save markyv18/da2f35701514b0420ebdd03156f50526 to your computer and use it in GitHub Desktop.
Save markyv18/da2f35701514b0420ebdd03156f50526 to your computer and use it in GitHub Desktop.
Week 1 Diagnostic
Create a gist and answer the following questions.
-------------------------------------------------------------
Floats and Integers
What’s the difference between a float and integer?
integer is a "whole" number whereas a float can contain a decimel point
What’s are the similarities and differences between BigNum and FixNum?
no decimels, both are integers. 18 or 19 character number is a BigNum - below that is FixNum
What will 4.0 / 2 return?
2.0
What will 1.5.to_i.to_f return?
1.5 to 1 to 1.0
What part of this expression ("hi" * 5?) is “syntactic sugar”? What does that mean?
n/a
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.
"hi " + "you" ---- "this is a string of #{num} things"
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.
will work but bad style = jasonloveschainsawsALOT
wont work 8isgreat
will work, good style = my_name_is
-------------------------------------------------------------
Arrays
How do you add to an array?
<< push unshift
What will [1,2,3].shuffle do?
shuffle the order of the items in the array
What do shift and unshift do?
remove and insert the first item in an array
Name 3 ways to retrieve 4 from this array: [4,3,5]
array[0]
array.shift
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 |item|
p "The word is '#{item}'"
end
-------------------------------------------------------------
Flow control
What’s the difference between a while and until loop?
while something is in this state do this thing, until this qualification is met do this this thing
How do you escape a pry in the middle of a loop?
exit
-------------------------------------------------------------
Enumerables (.each)
What is the purpose of an enumerable?
iterate over a group of items and perform functions on the individual items
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 = []
array.each do |item|
initials << item[0]
end
-------------------------------------------------------------
Classes and instance methods
When you’re defining a class, how do you store attributes?
instance variables (?)
What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do?
just reads value, can't read it only write to it, can read and write to it
What method corresponds with .new when you create a new instance of a class?
in-IT-ialize
-------------------------------------------------------------
Methods, arguments and scopes
Describe the scope that methods create. Are variables created in that scope accessible elsewhere?
local, no
What does the method scope have access to?
global variables and sibling methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment