Ruby Intro/ Methods Flashcards

1
Q

What is a method?

Why are methods so convenient?

A

a block of code written to perform a specific task

you only have to write a method once, but it can be called from other places in your program. also, you can make just one change in your method’s code, but that one change can affect many things in your program. you can give info to a method, and the method can give you info back

“keep all that code in one place”

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

What’s another word for “methods?”

A

functions

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

What does a parameter do?

A

establishes the the type of argument you must give the method

def say_hello(name)
puts (“Hello, “ + name)
end

“a method can accept parameters as input”

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

Here is a method:

def my_method(name)
puts (“Hello, “ + name)
puts (“Hi, “ + name)
end

What will print to the screen? Why?

A

“Hi, Joe”

because ruby will return the last returnable piece of info in the code

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

Here is a method:

def my_method(name)
puts (“Hello, “ + name)
puts (“Hi, “ + name)
end

If you want “Hello, Joe” to print to the screen, what word can you add to the code?

A

def my_method(name)
return puts (“Hello, “ + name)
puts (“Hi, “ + name)
end

my_method(“Joe”

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

in a method, do you have to add the keyword “return?”

A

no, returns are automatic in ruby, but you can add “return” to your code if you want to specify a certain line to return a value

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

does a method always need parameters?

A

no. you only need parameters if your method needs access to outside data

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

with regard to a method, what are “arguments?”

A

pieces of info sent to a method 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
9
Q

Here’s a method:

def my_method(name)
   puts ("Hello, " + name)
   puts ("Hi, " + name)
end
----------

Can the local variable “name” be referenced outside of the method definition?

A

no

outer scope cannot see inner scope

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

What does a default parameter do?

A

it assigns a value to the local variable if the caller doesn’t pass an argument to the method

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

There is only one way for a local variable in a method to gain access to data outside of the method? What is it?

A

The data must be passed to the method in the form of an argument

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