Skip to content

Instantly share code, notes, and snippets.

@wewert
Last active January 30, 2017 17:14
Show Gist options
  • Save wewert/293a3c6202a51fca28c9fcf6de310357 to your computer and use it in GitHub Desktop.
Save wewert/293a3c6202a51fca28c9fcf6de310357 to your computer and use it in GitHub Desktop.
Floats and Integers
What’s the difference between a float and integer? Floats have decimal places and Integers are whole numbers
Float example 1.55
Integer example 1
What’s are the similarities and differences between BigNum and FixNum?
They are whole numbers and they are within the same level
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 the multiplication. The algorithm that do this is compex and a lot more lines but is represented by just *
? is a conditional(true or false). The algorithm is complex and a lot of lines but is represented by just ?
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”?
String[1..3]
Give an example of string concatenation and string interpolation.
"Hello" + " " +"World"
a = "Hello"
b = "World"
puts "Here it is: #{a} #{b}"
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.
tHrEe_BlInD_mIcE
3blind_mice
three_blind_mice
Arrays
How do you add to an array?
<<
.push
What will [1,2,3].shuffle do?
shuffle the order of the items in the array
What do shift and unshift do?
shift puts in an item into the an array
unshift takes an itme out of an array
Name 3 ways to retrieve 4 from this array: [4,3,5]
[0]
.pop
.shift
How would you print each word from this list (["hello", "goodbye", "cactus"]) to terminal within this output:
The word is "hello".
print "The word is " arr[0]
The word is "goodbye".
print "The word is " arr[1]
The word is "cactus".
print "The word is " arr[2]
arr.map { |word| puts "The word is "#{word} }
Flow control
What’s the difference between a while and until loop?
while loop will continue to loop while the condition set is met. It hits true or false
Until loop will continue to loop until the condtion is met. It hits true
How do you escape a pry in the middle of a loop?
!!!
Enumerables (.each)
What is the purpose of an enumerable?
To make coding easier by not having to type out every method by hand
What are the two different ways that you can represent a block?
short hand for blocks that do only one thing
arr.each { |x| #do something }
long hand for blocks that do more
arr.each do |x|
#do something
#do something else
end
Given this array ["Beth", "Lauren", "Ilana"]. How would you create a new array of just the first initials of each name?
arr.map { |initial| initial[0] }
Classes and instance methods
When you’re defining a class, how do you store attributes?
class Class
initalize(att_1, att_2)
end
end
What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do?
attr_reader = read only
attr_writer = write only
attr_accessor = read and write
What method corresponds with .new when you create a new instance of a class?
initialize
Methods, arguments and scopes
Describe the scope that methods create. Are variables created in that scope accessible elsewhere?
Method scope create local variables that are not accessable to the class
What does the method scope have access to?
Method scope has access to sibling methods (other methods with in the same class)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment