Written Assessment Study Guide Flashcards
method definition
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.
Implicit Return Value of Method Invocations and Blocks
Methods and block implicitly return the last evaluated expression in the given method or block, unless we use explicit return.
Array#sort
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.
Is Array#sort mutating?
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.
Truthiness
Truthiness is a quality that describes whether an object will evaluate to true or false in a boolean context.
Which values are truthy?
All values are truthy, except for false or nil.
How do truthy/falsy variables evaluate in a boolean context?
Variables that are truthy evaluate to true in a boolean context.
Variables that are falsy evaluate to false in a boolean context.
Is truthy the same as true?
No. For instance, the value 1 is truthy, but 1 == true would evaluate to false.
Is falsy the same as false?
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.
Variables as pointers
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”.
Mutating strings
a = “hi”
b = a
a «_space;”, Bob”
We mutate the string referenced by a, which is also referenced by b. Therefore, evaluating b will return hi, Bob
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 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].
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)
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.
puts vs return
Ruby methods ALWAYS return the evaluated result of the last line of the expression unless an explicit return comes before it.
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
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.