Day 19 of Learning Ruby!

Alright, I’m back! Let’s jump in feet first:

Exercise 19: Functions and Variables

Here is what I typed:

and the result:

PS C:\ruby> ruby ex19.rb
We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man, that’s enough for a party!
Get a blanket.
OR, we can use variables from our scripts:
You have 10 cheeses!
You have 50 boxes of crackers!
Man, that’s enough for a party!
Get a blanket.
We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man, that’s enough for a party!
Get a blanket.
And we can combine the two, variables and math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man, that’s enough for a party!
Get a blanket.

Study Drills

1. Go back through the script and type a comment above each line explaining in English what it does.

# This function calls on the numbers given below, assigning them one after the other in order.

def cheese_and_crackers(cheese_count, boxes_of_crackers)
puts “You have #{cheese_count} cheeses!”
puts “You have #{boxes_of_crackers} boxes of crackers!”
puts “Man, that’s enough for a party!”
puts “Get a blanket.\n”
end

# This assigns the numbers to the #{cheese_count} and #{boxes_of_crackers} called above

puts “We can just give the function numbers directly:”
cheese_and_crackers(20, 30)

# These lines provide the function above alternate numbers, drawing on the following code.

puts “OR, we can use variables from our scripts:”
amount_of_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, amount_of_crackers)

# This code gives the function numbers in order using math

puts “We can even do math inside too:”
cheese_and_crackers(10 + 20, 5 + 6)

# And this combines the two. It takes the above variables and does math with it

puts “And we can combine the two, variables and math:”
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

2. Start at the bottom and read each line backward, saying all the important characters.

OK.

3. Write at least one more function of your own design, and run it 10 different ways.

I wasn’t able to get 10. The above did this:

PS C:\ruby> ruby studydrill19a.rb
Here we go:
We have 20 wizards able to defend the city!
There are 30 vampires plaguing the populace.
Let’s define them as variables:
We have 25 wizards able to defend the city!
There are 35 vampires plaguing the populace.
We have 1935 wizards able to defend the city!
There are 10 vampires plaguing the populace.
Another way!
We have 1.25 wizards able to defend the city!
There are 175 vampires plaguing the populace.

I, unfortunately started a new medication and it is knocking the life out of me. In the middle of working on this I passed out and slept a few hours. I am stopping the medications and getting my doctor to try something else! I may return tonight, but need to wake up.