Methods Flashcards

1
Q

parameter

A

used when you have data outside of a method definition’s scope, but you need access to it within the method definition. If the method definition does not need access to any outside data, I do not need to define any parameters.

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

argument

A

piece of information that is sent to a method invocation to be modified or used to return a specific result. We “pass” arguments to a method when we call it.

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

Note that the words local variable is ______ at the method definition level; that is you cannot reference this local variable outside of the say method definiton

A

scoped

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

default parameter

A

def say(words=’hello’)
puts words + ‘.’
end

say()
say("hi")
say("how are you")
say("I'm fine")
Notice that say() prints hello. to the console.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

method invocation with a block

A

Method invocation with a block

[1, 2, 3].each do |num|
puts num
end

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

mutating the caller

A

sometimes, when calling a method, the argument can be altered permanently

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

Method definition

A

Method definition

def print_num(num)
puts num
end

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

Two ways to call methods:

A
  1. The some_method(obj) is when you send arguments to a method call.
  2. Methods called with an explicit caller, like this a_caller.some_method(obj). Best to think of this as some_method modifying a_caller.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

string.upcase method

A

changes every lowercase letter to uppercase

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

string.downcase method

A

change every uppercase letter to lowercase

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

string.swapcase

A

switches the case of every letter in the string

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

string.capitalize

A

switches the first character to uppercase (if it’s a letter)

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

string.center

A

adds spaces to the beginning and end of the string to make it centered.

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

string. ljust

string. rjust

A

similar to center, but they pad the string with spaces on the right and left sides

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