w1d2 Flashcards

1
Q

What are 4 basic style tenets?

A

◦Indent your code.
◦Limit lines to 72 chars.
◦Avoid long methods and nesting more than two levels deep.
◦Don’t over-comment.

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

What is a good rule of thumb regarding DRY?

A

A good rule of thumb with DRY is that if you find yourself copying and pasting code into other places, you should most likely refactor to avoid duplication.

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

Explain what the =~ operator does.

A

Returns the first position in the (left-hand operator) string that matches the regex (right-hand operator)

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

Explain the ‘||’ trick.

A

Can be used to return the value on the right-hand side of the operator if the left-hand side evaluates to nil.

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

What is the recommended maximum number of lines for methods?

A

10

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

What property do the best methods have?

A

They are “atomic”; they do an single thing/have a single responsibility

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

What does making atomic methods and giving them logical & descriptive names do?

A

Makes our code (specifically our main function) EXTREMELY readable, to the point that anyone can read it like plain English to understand what it does. To see the specific implementation of a method, they can see the definition of that method.

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

T/F: Methods should not modify global variables, nor should they attempt to access values other than those given to them as arguments.

A

T

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

What is the best practice when writing a function to modify its given arguments?

A

They should only change the arguments if that is the EXPLICIT purpose of the method; otherwise, create a copy of the argument.

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

When should you use the following naming styles:

snake_case
CamelCase
SCREAMING_SNAKE_CASE
question_mark?
bang!
A
  • Use snake_case (not CamelCase ) when naming methods, variables, and files.
  • Use CamelCase for classes and modules.
  • Use SCREAMING_SNAKE_CASE for other constants.
  • Methods that return a boolean value (i.e. true or false ) should end with a question mark (e.g., Array#empty? ).
  • Methods that are potentially ‘dangerous’ (e.g., because they modify the calling object or arguments) should end with an exclamation mark (e.g., Array#map! changes the original array).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Name a few guidelines for choosing variable names.

A

Make names as long as necessary, but as short as possible.

For methods that have no side-effects and just return a value, try using a description of the return value (‘cosine’, ‘square_root’)

For methods that do have side-effects, try using a verb followed by a noun(‘print_cosine’, ‘record_square_root’)

Establish conventions for common operations (‘get_cosine’, ‘set_square_root’)

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

Say we have a set of objects that hold data of the the same types. What is an elegant solution?

A

Make an array of hashes with matching keys:

cats = [{
  :name => "Breakfast",
  :age => 8,
  :home_city => "San Francisco"
}, {
  :name => "Earl",
  :age => 2,
  :home_city => "San Francisco"
}]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a rule of thumb for using hashes?

A

Use a hash to collect variables that go together.

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

What is an instance?

A

An occurrence of an object.

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

What is a class?

A

A structure that combines state (data, fields) and behavior (methods).

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

T/F: Class names are capitalized and snake_cased.

A

F; Class names are capitalized and CamelCased.

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

Y/N: It is good for the ‘initialize’ method to perform actions in addition to setting data.

A

N; initialize should only handle data by setting up an object; logic can be put into a ‘run’ method.

18
Q

What is an instance method and how do we call it?

A

A method defined on an instance of a class, called as instanceName.methodName

19
Q

When should we use attr_reader over attr_accessor

A

When we want to prevent the user from changing an instance variable but still be able to see it.

20
Q

T/F: class methods cannot access class variables.

A

T; there is no instance they even have access to.

21
Q

What is a “factory method”?

A

A class method which creates a new object.

22
Q

What is the syntax for notating instance methods vs. class methods?

A

ClassName#instance_method

ClassName::class_method

23
Q

When might we want to define a class method?

A

When we want a factory method, or when we want a method doesn’t rely on instance data.

24
Q

Y/N: Can we omit the ‘self’ when calling instance/class methods from within the object?

A

Y; Ruby will add the ‘self’ automatically, and this is the preferred style.

25
Q

What is the difference between Kernel#puts and Kernel#print ?

A

‘puts’ appends the newline character, ‘print’ does not.

26
Q

T/F: Kernel#puts appends a newline after each element in an array.

A

T

27
Q

What are the differences between all of the File class methods:

File::open
File::foreach
File::read
File::readlines
File::new
A

‘open’ opens a file and takes a block. The block can then use File#gets.chomp to get the FIRST line of the file.

‘foreach’ takes a block and does something once per line

‘read’ returns the contents of a file as one long string

‘readlines’ returns each line as an array of strings, with each element containing a newline at the end.

‘new’ creates a new file, which can then be used with the methods from ‘Enumerable’:

f = File.new(“todos”)
f.each {|line| puts “#{f.lineno}: #{line}” }

28
Q

How do we open a file in append mode, and how doe we open a file in write mode? What is the difference?

A

File.open(“filename”, “a”)
File.open(“filename”, “w”)

Note that each of these takes a block, and then uses that block to put things into the file (can use File#puts for this purpose)

Write mode will delete the existing contents of the file, whereas append mode will not.

29
Q

What is the method to close a file?

A

File#close

30
Q

T/F: $stdin and $stdout are just File objects.

A

T; this is why they share gets and puts methods, b/c the Kernel is actually calling these as instance methods from the two constants.

31
Q

How do we access command line arguments from within a script?

A

the ARGV array of strings.

32
Q

When should a while loop be used.

A

When we want to repeat an action until something happens, but we don’t know when that something will happen.

33
Q

What is the keyword to break out of a loop?

A

‘break’

34
Q

What is Enumerator#each_with_index , and how do we use it?

A

It is an each method that automatically keeps track of the index:

my_favorite_number = 42
numbers = [42, 3, 42, 5]
favorite_indices = []
numbers.each_with_index do |number, index|
  if number == my_favorite_number
    favorite_indices << index
  end
end
35
Q

Y/N: Can we use Enumerator#each with ranges?

A

Y

36
Q

What is one smart way to use an explicit return to exit a function early if a certain condition is met?

A

Use ‘unless’:

def go_home
  return unless can_go_home? && wants_to_go_home?

pack_bags
get_tickets
board_plane
end

37
Q

Are return values always meaningful?

A

Not always; sometimes we just want a method for its side-effects.

38
Q

Why does the || trick work?

A

Short-circuiting

39
Q

What is the following shorthand for:

@values[n] ||= calculate_fib(n)

A

@values[n] = @values[n] || calculate_fib(n)

40
Q

T/F: Ruby is a “pass by reference” language.

A

T

41
Q

What are symbols used for?

A

To represent the names of things; they are NOT intended for input/output. Used when representing data not meant for I/O

42
Q

Where are options hashes declared, and how are they declared?

A

They must be the LAST argument, and are declared as options = {}