Skip to content

Instantly share code, notes, and snippets.

@caward12
Last active January 30, 2017 17:07
Show Gist options
  • Save caward12/2c5d672a4ebe63f3633ef2c3345d7e08 to your computer and use it in GitHub Desktop.
Save caward12/2c5d672a4ebe63f3633ef2c3345d7e08 to your computer and use it in GitHub Desktop.
Week 1 Diagnostic
Floats and Integers
What’s the difference between a float and integer?
>a float holds decimals and an integer does not
What’s are the similarities and differences between BigNum and FixNum?
>They are similar in that they both inherit from the integer and number classes, neither can accomodate decimals. and they are different in that BigNum is for really large numbers (18,19,20 numbers long)
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?
> * is "syntactic sugar" - it shortens the expression you are typing - they are shortcuts to make code more readable
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] - meant as name_of_string(1..3)
Give an example of string concatenation and string interpolation.
> concatenation "hello" + " world!" interpolation "hello #{world}!" (assuming world is a variable you set equal to "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" -FirstModule "invalid" - 1module "conventional" - first_module
Arrays
How do you add to an array?
> either use .push or << .unshift (takes argument and adds to the begining)
What will [1,2,3].shuffle do?
> shuffle the elements randomly and return array in new order
What do shift and unshift do?
> .unshift adds and .shift removes(and returns) the first element in an array
Name 3 ways to retrieve 4 from this array: [4,3,5]
> array.first, array[0], or array[-3]
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 { |item| puts 'the word is "#{item}"' }
Flow control
What’s the difference between a while and until loop?
>while is used while a condition is met(condition is true) and until is used unitl a condition is met (condition is false)
How do you escape a pry in the middle of a loop?
>exit
Enumerables (.each)
What is the purpose of an enumerable?
>to loop through each item in a collection and do something to it
What are the two different ways that you can represent a block?
> do | |
do something
end
and then { | | do something }
Given this array ["Beth", "Lauren", "Ilana"]. How would you create a new array of just the first initials of each name?
>array.map { |name| name[0] }
Classes and instance methods
When you’re defining a class, how do you store attributes?
>as instance variables in the initializer method
def initialize (attribute)
@attribute = attribute
end
What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do?
>attr_readers set up "getter" methods - lets you access the instance variables, attr_writers lets you "set" (or modify) instance variables, and attr_accessors let you do both get and set instance variables
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?
>methods create a local scope. variables created inside the method are not accessible outside of the local scope (outside of that method)
What does the method scope have access to?
>the method scope has access to its own method and then any sister methods in the same class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment