Skip to content

Instantly share code, notes, and snippets.

@BethKnight1234
Last active January 30, 2017 17:07
Show Gist options
  • Save BethKnight1234/4eef62082dee96ed433bc6464b2662bd to your computer and use it in GitHub Desktop.
Save BethKnight1234/4eef62082dee96ed433bc6464b2662bd to your computer and use it in GitHub Desktop.
Week 1 Diagnostic
FLOATS AND INTEGERS
* What’s the difference between a float and integer?
Integer is a rounded whole number. A float includes the whole decimal number.
* What’s are the similarities and differences between BigNum and FixNum?
BigNum is a really big number, like 23894720340193423. Fixnum is smaller? They’re both integers.
* 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?
Syntax sugar means it looks prettier. The sugar in that expression is * that dude.
* 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”?
x = “hello”
x[1, 3]
* Give an example of string concatenation and string interpolation.
x = “hello”
“#{x} world!”
x + “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 = valid
invalid = InvALid NamE*
valid/coventional = valid_and_stylistically_conventional
ARRAYS
* How do you add to an array?
array << new_object
* What will [1,2,3].shuffle do?
mix’em up!
* What do shift and unshift do?
Shift removes the first item of an array. Unshift adds items to the front of an array.
* Name 3 ways to retrieve 4 from this array: [4,3,5]
Shift, [0], first
* How would you print each word from this list (["hello", "goodbye", "cactus"]) to terminal within this output:
things = [“hello", "goodbye", "cactus"]
things.each do |word|
puts "the word is #{word}"
end
FLOW CONTROL
* What’s the difference between a while and until loop?
While loops do something while the above parameters are true, such as x < 5.
Until loops do something until the condition is met.
* How do you escape a pry in the middle of a loop?
exit
ENUMERABLES (.each)
* What is the purpose of an enumerable?
It iterates over a collection of data, an array, and does stuff to it. It can either do things to the individual objects or put them into a new array after it’s changed them.
* What are the two different ways that you can represent a block?
array.each do |x|
puts x
end
array.each { |x| puts x }
* Given this array ["Beth", "Lauren", "Ilana"]. How would you create a new array of just the first initials of each name?
names = ["Beth", "Lauren", "Ilana"].
names.map { |x| puts x[0] }
CLASSES AND INSTANCE METHODS
* When you’re defining a class, how do you store attributes?
In attribute_reader/writer/accessors
* What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do?
Reader saves it, doesn’t change it. Writer probably does the same.
Accessors let you change it.
* What method corresponds with .new when you create a new instance of a class?
Initialize method
METHODS, ARGUMNETS, AND SCOPES
* Describe the scope that methods create. Are variables created in that scope accessible elsewhere?
Methods can only be used in the class they’re called in. You can create global variables that are accessible everywhere.
* What does the method scope have access to?
It’s own variables and arguments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment