Written Assessment Study Guide Flashcards

1
Q

method definition

A

A method definition consists of the keyword def followed by the method name, optional parameter, optional body, and the end keyword. A method invocation involves an object calling the method and (optionally) passing in arguments.

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

Implicit Return Value of Method Invocations and Blocks

A

Methods and block implicitly return the last evaluated expression in the given method or block, unless we use explicit return.

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

Array#sort

A

The #sort method implements the spaceship operator, <=> to compare and sort elements in a collection. By default, the sort method will order elements in non-descending order (least to greatest). If you pass in a block, you can change the ordering of the elements.

<=> is a method that accepts two operands, the calling object and an argument, e.g. 1 and 2 in 1 <=> 2. The spaceship operator returns one of 4 values when comparing operands:
-1 When the calling object (left operand) is less than the right, e.g. 1 <=> 2
0, when the calling object and right operand have the same value.e.g., 1<=>1
1, when the calling object has a greater value than the right operand. 2<=>1
nil, when the calling object and the right operand are of different data types and uncomparable, e.g. 31 <=> ‘12’, we are unable to use the spaceship operator to compare a number with a string*. *unless you override the spaceship operator and redefine the operator.

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

Is Array#sort mutating?

A

No. This sort method will return a new array containing elements from the calling object that we’ve sorted according to the value of the block.

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

Truthiness

A

Truthiness is a quality that describes whether an object will evaluate to true or false in a boolean context.

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

Which values are truthy?

A

All values are truthy, except for false or nil.

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

How do truthy/falsy variables evaluate in a boolean context?

A

Variables that are truthy evaluate to true in a boolean context.
Variables that are falsy evaluate to false in a boolean context.

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

Is truthy the same as true?

A

No. For instance, the value 1 is truthy, but 1 == true would evaluate to false.

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

Is falsy the same as false?

A

No. Falsy is not the same as false. For instance, nil is falsy, but nil == false would evaluate to false because nil and false are different objects.

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

Variables as pointers

A

Variables store pointers, not values, to an address spaces in memory.

a = “hi there”
b = a
a = “not there”
We reassign a to “not there”. However, evaluating b will still return “hi there” because b contains a pointer to “hi there”.

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

Mutating strings

a = “hi”
b = a
a &laquo_space;”, Bob”

A

We mutate the string referenced by a, which is also referenced by b. Therefore, evaluating b will return hi, Bob

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

What’s a, b, and c? What if the last line was c = a.uniq!?

a = [1, 2, 3, 3]
b = a
c = a.uniq

A

a and b point to [1,2,3,3]. c points to [1,2,3]. If the last line was c = a.uniq!, then a, b, and c would point to [1,2,3].

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

What is a after the test method returns? Did the method modify the value of a?

def test(b)
b.map {|letter| “I like the letter: #{letter}”}
end

a = [‘a’, ‘b’, ‘c’]
test(a)

A

When we use variables to pass arguments to a method, we’re essentially assigning the value of the original variable (a in this case) to a variable inside the method (b). This is equivalent to executing b = a. Inside the method, the operations we perform on the b variable determine whether the value of a will change. Some operations, like map, will have no affect on a. Others, like map! will mutate the value assigned to a.

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

puts vs return

A

Ruby methods ALWAYS return the evaluated result of the last line of the expression unless an explicit return comes before it.

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

What happens when we add an explicit return to this code?

def add_three(number)
return number + 3
number + 4
end

returned_value = add_three(4)
puts returned_value

A

We output 7 because we return the value of number + 3 out of the add_three method using the return keyword. We don’t execute number + 4.

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

When are hashes better than arrays?

A

When you want to quickly access specific data without having to know the exact index or searching through the pile of data one by one.

17
Q

Array#map

A

The map method takes each element of an array, passes [the element] to a block, and builds a new array out of the values the block returns.

The map method adds the block’s return value itself to a new array.

18
Q

def map
new_arr = []
self.each do |element|
new_arr &laquo_space;yield(element)
end
new_arr
end

some_array.map do |element|
# does something
end

A

An implementation for map could look like the above. What’s happening is that map will iterate over each element in the collection we invoked map on and pass in the element to a do..end block. Within this block, we yield control to a block and pass in the element as an argument to the block. The block then returns a value back to yield, and we add this return value to the new_arr. At the end, map returns a new array consisting of the elements returned by the block passed into map.

19
Q

select method

A

select evaluates the return value of the block. The block returns a value on each iteration, which then gets evaluated by select. select only cares about the truthiness of the block’s return value. If the return value of the block is “truthy”, then the element during that iteration will be selected. If the return value of the block is “false” then the element will not be selected.

20
Q

each method

A

each sends the value of the current element to the block in the form of an argument. In this block, the argument to the block is num and it represents the value of the current element in the array. The code within the block is executed for each iteration.