Methods Flashcards

1
Q

What are methods and why do we need them?

A

A piece of code that needs to be executed many times in a program

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

What must we do before we can use a method?

A

Define it using the reserved word [def], name it and use [end] to complete

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

How do we invoke a method?

A

By typing it’s name and passing it in arguments

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

When would you use a parameter?

A

When you have data outside of a method definition’s scope, but you need to access it within a method’s definition

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

What is an argument?

A

Pieces of information that are sent to a method invocation to be modified or used to return a specific result

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

When do we pass arguments to a method?

A

When we call the method

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

What is the benefit of a method?

A

Gives us the ability to make changes in one place that affect many places in our program

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

What is the purpose of a default parameter?

A

So that the method always works whether it is given an argument or not

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

Where does a method definition create its scope?

A

Outside the regular flow of execution

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

How do you call a method?

A

Either the some_method(obj) call or a_caller.some_method(obj) call

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

Can methods be altered permanently outside of the definition?

A

Yes, by mutating the caller

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

True or False: Ruby methods always returns the evaluated result of the last line of the expression unless an explicit return comes before it

A

True

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

How do we explicitly return a value?

A

By using the keyword [return]

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

Is the reserved word [return] required in order to return something from a method?

A

No

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

Why are we able to chain methods together?

A

Because every method call returns a value

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

If a method returns the value “nil” will we be able to chain methods?

A

No. Because “nil” doesn’t know how to respond to further calls

17
Q

True or false: we can pass method call as an argument

A

True

18
Q

What does the call stack do?

A

Helps your programming language keep track of what method is executing as well as where execution should resume when it returns

19
Q

Aside from methods, what else uses the call stack?

A

Blocks, procs and lambdas

20
Q

True or false: all Ruby methods (including built in ones like [puts]), share the same call stack

A

True

21
Q

When does a stack frame get popped from the call stack?

A

When a method returns a value

22
Q

When does a program end?

A

When the main method has no more code to run and gets popped from the call stack