Methods, Blocks & Sorting Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

method

A

a reusable section of code written to execute a certain task.
defined using “def”

i.e.
def greeting
puts “hello”
end

greeting
-> hello

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

three parts to a method

A
  1. def & name of the method
  2. body: describes the procedure of the methos
  3. end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

parameter

A

placeholder for the method argument

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

argument

A

the argument you use in running a method

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

splat argument

A

a method that can receive one or more arguments.

use * to signal to an parameter that it can receive several arguments

i.e.

def what_up(greeting, *friends)

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

return

A

returns back a value from a method

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

blocks

A

nameless methods.
define by either do and end
or { }

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

difference between methods and blocks

A

methods can be called several times whereas blocks can only be called once

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

.sort!

A

Sorts values in an array either numerically or alphabetically

my_array = [3, 4, 8, 7, 1, 6, 5, 9, 2]

puts my_array.sort!

prints out
[1, 2, 3, 4, 5, 6, 7, 8, 9]

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

<=>

A

combines comparison operator.
compares to objects
It returns 0 if the first operand (item to be compared) equals the second, 1 if the first operand is greater than the second, and -1 if the first operand is less than the second.

i.e.

book_1 = “A Wrinkle in Time”

book_2 = “A Brief History of Time”

book_1 <=> book_2

returns 1

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

.reverse!

A

reverse sorts and array from last to first

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