w1d2 Flashcards

1
Q

How do you require a file from the same directory as the file doing the require?

A

require_relative

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does require_relative do?

A

It loads a file from the same folder as the requiring file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

High-level: given an array, how would you find pairs of elements which have a sum of 0?

A

outer loop for the i to array.length element
inner loop for the i+i to array.length element
collect the pairs that sum to zero

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does Array#take do?

A

Returns first n elements from the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which method returns the first n elements from the array?

A

Array#take

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does Array#drop do?

A

Returns last n elements from the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which method returns the last n elements from the array

A

Array#drop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the problem with doing this:

a = Array.new(3, [ ] )
a[0] &laquo_space;“Hello”

What’s a better way? Why?

A

The empty array we pass to the Array constructor is actually a reference to an empty array. Each of the 3 elements in a will reference the same array. When we assign “Hello” to a[0], we actually assign it to all 3 elements.

Better way: a = Array.new(3) { [ ] }

This creates a new empty array reference for each element of a.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s the difference between the assignment operator in Ruby and C?

Describe what this does:
a = 4
a = 6

A

In Ruby, = does not mutate an object. Instead, it reassigns the variable so that it points to a new object.

a = 4 calls Fixnum.new(4), stores it somewhere, then returns a reference to this Fixnum and assigns it to a.

a = 6 calls Fixnum a second time, creating a second Fixnum, and changes a to point at the second Fixnum.

The original Fixnum(4) is still out there, until GC gets it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is syntactic sugar? Give an example

A

Shortcuts in the Ruby language that makes writing code easier.

x += 5

This does not call a special += method, instead, it is rewritten internally as:

x = x + 5

and then executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you conditionally assign something?

A

Use the ||= operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What type of default value is safe to pass to a Hash or Array?

A

An immutable object. Fixnum or Float is safe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What output do we get here (line-by-line):

cats = Hash.new( [ ] )
cats[“John”] &laquo_space;“kiki”
cats
cats[“Bob”]

A

{ }
[“kiki”]
{ }
[“kiki”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the proper way to initialize a hash of arrays? Why do we use this syntax? What happens if we don’t?

A

a = Hash.new { |hash, key| hash[key] = [ ] }

We do this in order to create a new array reference for each new hash element.

If we don’t do the hash[key] = [ ] part, a reference to the new array will not be stored in the hash.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List 9 code smells

8-9: 5 stars
5-7: 4 stars
4-5: 3 stars
2-3: 2 stars
0-1: 1 star
A
Duplicated code
Long methods
Too many parameters
Data clump
Long method chains
Indecent Exposure
Speculative Generality
God object
Dead code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does Indecent Exposure indicate?

A

A Class is sharing way too much with the outside world. This indicates heavy coupling with outside objects.

It can also indicate that the class is doing too much.

17
Q

What does Speculative Generality indicate? How can the mindset behind this be adjusted?

A

The code is far too generalized for its current purpose. Too many future-looking features have been implemented.

YAGNI: You ain’t gonna need it!

Don’t solve abstract problems until you have a concrete problem.

18
Q

How do you slurp in a whole file in one go?

A

File.read(‘filename’)

19
Q

When do you have to manually close a file?

Example?

A

Whenever you manually open a file outside of a block.

Example: f = File.open(‘filename’)

20
Q

How do you write a [ ] accessor method for a single-dimensional array?

A

def
self[idx]
end

21
Q

How do you write a [ ]= assignment method for a single-dimensional array?

A
def [ ]=(idx, val)
  self[idx] = val
22
Q

How do you open the command palette in Atom?

A

cmd + shift + p

23
Q

What does cmd + shift + p do in Atom?

A

Opens the command palette.