Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Week 1 Diagnostic

start time: 10:22
end time: 10:45
total: 24 min

Floats and Integers

What’s the difference between a float and integer?

Floats can have many decimals, integers have no decimals.

What’s are the similarities and differences between BigNum and FixNum? BigNum is for numbers ~19 digits long and up. Fixnum is for numbers less than 19 digits long. (Seems that any number with decimals will be a float, regardless of length.)

What will 4.0 / 2 return?

2.0

What will 1.5.to_i.to_f return?

1.5

What part of this expression ("hi" * 5?) is “syntactic sugar”? What does that mean?

the * is short for .times. and I don't know what the ? does, because I cannot run it in IRB. I don't know if its a typo, or tries to evaluate the result to a boolean.

What will 10 % 3 return?

3

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".take(1..-1)

Give an example of string concatenation and string interpolation.

name = "Josh"
day = "Friday"

puts "Hi #{name}, today is ", day

^^ might not be right, but since I'm avoiding IRB, there's possibly minor formatting things. (The + sign threw errors for me recently on the issue of string concatonation. I don't remember why.)

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.

StudentName = "Josh" <-- valid but bad style 1st_letter = "A" <-- invalid, I believe student_name = "Josh" <-- snake_case.

Arrays

How do you add to an array?

array = []
array << "item"
array.unshift("item2")
array.push("item3")

What will [1,2,3].shuffle do?

shuffle the items in array.

What do shift and unshift do?

shift "pops" the first item in the array unshift pushes an item into index0 of the array

Name 3 ways to retrieve 4 from this array: [4,3,5]

array = [4,3,5]
array.shift => 4
array[0] => 4
array.first => 4

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".

list = ["hello", "goodbye", "cactus"]
list.each do |word|
  p "The word is #{word}."
 end

Flow control

What’s the difference between a while and until loop?

while runs as long as a condition evaluates to t/f (whatever you tell it) until runs until some other condition evaluates to whatever you tell it.

while switch == true {do all this} until switch == true {do all this}

How do you escape a pry in the middle of a loop? !!! or quit or ctrl-c or ctrl-d. Sometimes !!! works fine, other times it doesnt. proceed down the list until you're out.

Enumerables (.each)

What is the purpose of an enumerable?

to iterate through a collection (arrays or hashes)

What are the two different ways that you can represent a block?

array.map do |item|
  item * 2
end

array.map { |item| item * 2 }

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"]
results = names.map do |name|
  name[0]
end

Classes and instance methods

When you’re defining a class, how do you store attributes?

using @instance_variables that you can either accept arguments for from the thing = Class.new(variable1, variable2) command, or you can just initialize them to a default value.

What is the difference between attr_readers, attr_writers, and attr_accessors? What do each do?

attr_reader can only read instance variables. attr_writer can only write instance variables attr_accessor can read and write instance variables (as methods. I.E. student.name can be read, written, or both, depending on which attr_ you use.

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?

variables inside of a scope are not accessible from outside of it, though they can change values of variables defined outside of their scope.

What does the method scope have access to?

  • other variables inside the scope
  • variables defined outside the method
  • NOT variables defined inside other methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment