Per the body of this Bloc checkpoint, here are the two requested message assignments.
Note: this does not include the actual assignments listed under the Assignement tab. I will include those details in my final Submission.
Toaster Function
If I were to write a method that executes the functionality of an actual toaster, it might read something like this:
- The keyword
def
tells Ruby that I am about to define a method. toaster
is the name assigned to the method.food
inside of a parenthesis is the parameter. Arguments passed through when the method is actually called must be edible items.- Inside the block of code, I might include a conditional that tells the program
if the argument passed through is not an edible item, return ‘Stop, that item cannot be toasted!’ else run code that will actually toast the item and return, ‘be careful, it’s hot!’
- The key work
end
tells Ruby to close the function
Method Example: peer_pressure
My initial thoughts before running the code in the peer_pressure
example were this:
Define Variable & Method:
- First, we are defining a variable,
favorite_social_network
, and setting it to refer to the value“Myspace”
- Then, we are defining a method,
peer_pressure
. Within thepeer_pressure
method, we are re-assigning the variablefavorite_social_network
to refer to the value“Facebook”
and then instructing the program to print that value.
The Results:
- Now, if you call the method
peer_pressure
(line 8) you should get:“Facebook”
- If you print the true value of
favorite_social_network
(line 9) you should get:“Facebook”
- This is because we reset the value of
favorite_social_network
to“Facebook”
in thepeer_pressure
method so anytime we inspect the value of that variable, we will get the most recently assigned value because variables “can be reassigned at any time and will only keep the latest assigned value.” (Bloc Checkpoint 1: Intro to Ruby)
- This is because we reset the value of
Inaccurate Thought Process:
- I ran the code to validate my thinking and found that no matter where in the code I run the line
p favorite_social_network
I get“Myspace”
.
Requested Assistance:
- I am not understanding why assigning the value of the variable
favorite_social_network
to“Facebook”
in thepeer_pressure
method is not updating the true value of the variablefavorite_social_network
when inspected after the method has been called. - I have done some research and can’t find an answer. Perhaps you can help me figure out what specific terms I should be Googling OR we can discuss in our upcoming mentor meeting.
Thanks!