w1d1 Flashcards

1
Q

What is a method?

A

A block of code referred to by name.

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

What’s the difference between a method and a function?

A

A method is associated with an object.

A function stands alone.

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

Does ruby have functions? Why or why not?

A

Not strictly. All functions in ruby are actually methods as they are all associated with an object.

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

In regards to a method, what is meant by its “side effects” ? What is a commonly used function with a side effect?

A

A side effect is some sort of modification to a non-local variable or some sort of observable interaction with the outside world.

Example: puts

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

What is an implicit return? Is this preferred? Why or why not?

A

The last evaluated expression in a method is the return value of that method.

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

What are three signs of a good method?

A

It does one thing.

It has a short description and a good name.

It’s body is under 10 lines long.

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

How many lines should a method be limited to?

A

10

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

What is an object?

A

A convenient way of grouping data and behavior.

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

Data and behavior is grouped together within a..

A

object

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

What in ruby is not an object?

A

Trick question: everything is an object.

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

What is the parent class for all objects in Ruby?

A

Object

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

Describe Object’s relation to other classes in Ruby.

A

Object is the parent class for all other classes.

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

What’s the difference between calling puts and p on an object?

A

puts returns object.to_s

p returns object.inspect

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

Why does nil.nil? return true?

A

Object#nil?

returns false, while only NilClass#nil? returns true

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

How do you test to see if an object is an instance of a particular class?

A

Object#is_a?

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

What module is mixed into every class?

A

Kernel

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

Why are methods like puts and gets available to any context?

A

The Kernel module is mixed into every class.

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

Array#each is a bad way to delete elements within an array. What’s a better method?

A

Array#delete_if

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

How do we tell if an array has 0 elements?

A

Array#empty?

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

What two methods are used when treating an Array as a Queue?

A

Array#push

Array#shift

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

What’s a naming technique that helps the user known that a variable is an array?

A

Make it plural.

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

Why is an array variable name typically pluralized?

A

To help the user know that the variable represents an array.

23
Q

Why not mix types in an array?

A

It makes it difficult to iterate when the type of the item changes.

24
Q

Why is string interpolation preferred over concatenation?

A

It’s easier to read.

25
Q

str = “asdfjkl”

What’s a way to access the substring “dfj” of str using only the bracket operator?

A

str[2,3]

26
Q

How do you replace all instances of a pattern within a string?

A

String#gsub

27
Q

How do you set up multiple replacements within a single call to String#gsub?

A

pass it a hash where the keys are the matches and the values are the replacements.

28
Q

How do you convert a semi-string, i.e. “123 Flatbush” into an integer? What will it return in this example?

A

String#to_i

123

29
Q

What’s the purpose of String#to_sym?

A

Return a symbol that points to the string.

30
Q

What does nil.to_s return?

A

”” (empty string)

31
Q

What’s the difference between:

bundle exec rspec
rspec

A

The first changes PATH to the local directory. This causes the local RSpec version to be used.

The second uses the system’s version of RSpec, which may be out of date or incompatible with the spec’s.

32
Q

Rule of thumb: how much longer does it take to debug than to write code?

A

10x

33
Q

What’s the term for an input that produces the wrong output?

A

a regression

34
Q

How do you step into a method call in byebug?

A

step

35
Q

How do you step out of a method call in byebug?

A

finish

36
Q

What are two cases where a NameError might be thrown? What are their error messages? When are they each thrown?

A

NameError: undefined local variable – thrown when using a variable or method that hasn’t been defined

NameError: uninitialized constant – thrown when a class or other constant is referenced but cannot be found.

37
Q

when using a variable or method that hasn’t been defined, what Exception will be thrown, and with what error message?

A

NameError: undefined local variable

38
Q

when using a Class that can’t be found by the interpreter, what Exception will be thrown, and with what error message?

A

NameError: uninitialized constant

39
Q

When might a NoMethodError be thrown?

A

when it’s clear that the user tried to call a method (not a variable) that doesn’t exist.

40
Q

when it’s clear that the user tried to call a method (not a variable) that doesn’t exist., what error is thrown?

A

NoMethodError

41
Q

If we don’t give a method the right number of arguments, what exception will be thrown at us? What error message?

A

ArgumentError: wrong number of arguments

42
Q

When might a TypeError be thrown? What’s a typical error message?

A

if you pass the wrong type of thing to a method.

TypeError: String can’t be coerced into Fixnum

43
Q

if you pass the wrong type of thing to a method, what error is thrown? What error message?

A

TypeError: can’t be coerced into

44
Q

What are two cases where a LoadError might be thrown? What’s a typical error message?

A

LoadError: cannot load such file –

This can happen when you’re requiring a gem that hasn’t been installed.

This can also happen when you’re trying to require a file without using the initial “./”

45
Q

What error is thrown when you’re requiring a gem that hasn’t been installed?

A

LoadError

46
Q

What error is thrown when you’re trying to require a file without using the initial “./”

A

LoadError

47
Q

Writing ungrammatical code will throw what type of error?

A

SyntaxError

48
Q

What are 3 common causes of SyntaxErrors?

A

forgetting to close quotes

forgetting to close parentheses

forgetting to close do-end blocks

49
Q

What caused this error:

SyntaxError: /Users/ruggeri/test.rb:4: syntax error, unexpected keyword_end, expecting $end

A

An extra end statement

50
Q

What caused this error:

SyntaxError: /Users/ruggeri/test.rb:3: syntax error, unexpected $end, expecting keyword_end

A

A missing end statement

51
Q

An extra end statement will throw what error?

A

SyntaxError: /Users/ruggeri/test.rb:4: syntax error, unexpected keyword_end, expecting $end

52
Q

A missing end statement will throw what error:

A

SyntaxError: /Users/ruggeri/test.rb:3: syntax error, unexpected $end, expecting keyword_end

53
Q

How do you call a block with a single parameter ‘name’ from a method?

A

within the method, use:

yield(name)