diff --git a/day_1/ex1.rb b/day_1/ex1.rb new file mode 100644 index 000000000..f96a35d21 --- /dev/null +++ b/day_1/ex1.rb @@ -0,0 +1,9 @@ +#puts "Hello World!" +#puts "Hello Again" +#puts "I like typing this." +#puts "This is fun." +puts "Yay! Printing." +#puts "I'd much rather you 'not'." +#puts 'I "said" do not touch this.' +#puts "I am interested in why a double quotation and a single quotation were used." +#puts "This looks messy to me." diff --git a/day_1/ex2.rb b/day_1/ex2.rb new file mode 100644 index 000000000..7bfeb3f03 --- /dev/null +++ b/day_1/ex2.rb @@ -0,0 +1,12 @@ +# A comment, this is so you can read your program later. + +# Anything after the # is ignored by ruby. + + +puts "I could have code like this." # and the comment after is ignored + + +# You can also use a comment to "disable" or comment out a piece of code: +# puts "This won't run." + +puts "This will run." diff --git a/day_1/ex3.rb b/day_1/ex3.rb new file mode 100644 index 000000000..c91c32380 --- /dev/null +++ b/day_1/ex3.rb @@ -0,0 +1,34 @@ +# This prints out the line below +puts "I will now count my chickens:" + +# This is a math equation, first dividing 30 & 6 then adding 25. +puts "Hens #{25.0 + 30.0 / 6.0}" +# This is a math equation, I'm not quite sure what the whole equation is doing. I'm a little confused about what to do with the remaing 4. +# Added in later: I was able to dive into the % and feel more confident with it. +puts "Roosters #{100.0 - 25.0 * 3.0 % 4.0}" +# This prints out the line below. +puts "Now I will count the eggs:" + +# This is a math equation that states the amount of eggs available. +puts 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0 + +# This a question posed +puts "Is it true that 3.0 + 2.0 < 5.0 - 7.0?" + +# This is the answer to the above stated question +puts 3.0 + 2.0 < 5.0 - 7.0 + +# These next two lines are indicating the break down of the above equation to find out why it's answered as false +puts "What is 3.0 + 2.0? #{3.0 + 2.0}" +puts "What is 5.0 - 7.0? #{5.0 - 7.0}" + +# This prints out the line below +puts "Oh, that's why it's false." + +# This prints out the line below +puts "How about some more." + +# The next three lines are answering boolean logic, which is why there is a 'true' or 'false' stated instead of the answer to the equation. +puts "Is it greater? #{5.0 > -2.0}" +puts "Is it greater or equal? #{5.0 >= -2.0}" +puts "Is it less or equal? #{5.0 <= -2.0}" diff --git a/day_1/ex4.rb b/day_1/ex4.rb new file mode 100644 index 000000000..a601972f1 --- /dev/null +++ b/day_1/ex4.rb @@ -0,0 +1,30 @@ +# assign an integer for cars variable +cars = 100 +# assign a float for the space_in_a_car variable +space_in_a_car = 4.0 +# assign an integer for drivers variable +drivers = 30 +# assign an integer for passengers variables +passengers = 90 +# use the above assigned variables to create an equation +cars_not_driven = cars - drivers +# assign the drivers variable amount to this variable name +cars_driven = drivers +# create an equaiton of above assigned variables +carpool_capacity = cars_driven * space_in_a_car +# create an equation of above assigned variables +average_passengers_per_car = passengers / cars_driven + +# utilizing the assigned variable to complete the phrase +puts "There are #{cars} cars available." +puts "There are only #{drivers} drivers available." +# utilizing the assigned variable to complete the phrase, an eqation was done above to reach this number +puts "There will be #{cars_not_driven} empty cars today." +puts "We can transport #{carpool_capacity} people today." +puts "We have #{passengers} to carpool today." +puts "We need to put about #{average_passengers_per_car} in each car." + + +# Study drills + +# The mistake that was given I was able to recreate by not putting double quotations around line 14. There was a specification for what line 14 was being asked to do. diff --git a/day_1/ex5.rb b/day_1/ex5.rb new file mode 100644 index 000000000..2d337470b --- /dev/null +++ b/day_1/ex5.rb @@ -0,0 +1,24 @@ +name = 'Zed A. Shaw' +age = 35 # not a lie in 2009 +height = 74 # inches +weight = 180 # lbs +eyes = 'Blue' +teeth = 'White' +hair = 'Brown' +centimeter = 2.54 +kilogram = 2.2046 +rounded = 81.6 + +puts "Let's talk about #{name}." +puts "He's #{height} inches tall." +puts "He's #{weight} pounds heavy." +puts "Actually that's not too heavy." +puts "He's got #{eyes} eyes and #{hair} hair." +puts "His teeth are usually #{teeth} depending on the coffee." + +# this line is tricky, try to get it exactly right +puts "If I add #{age}, #{height}, and #{weight} I get #{age + height + weight}." +puts "#{name} is #{height * centimeter} centimeters tall." +puts "#{name} is #{weight / kilogram} kilograms." +puts "Wow, #{weight / kilogram} kilograms is quite a number!" +puts "Let's round that out to #{rounded} kilograms, that's better." diff --git a/day_1/ex6.rb b/day_1/ex6.rb new file mode 100644 index 000000000..85a755e0b --- /dev/null +++ b/day_1/ex6.rb @@ -0,0 +1,37 @@ +# assign integer to variable +types_of_people = 10 +# assign string to variable with integer assigned in string +x = "There are #{types_of_people} types of people." +# assign string to variable +binary = "binary" +# assign string to variable +do_not = "don't" +# assign string to variable with two separate strings assigned within +y = "Those who know #{binary} and those who #{do_not}." +# print out above assigned variables +puts x +# print out above assigned variables +puts y +# print out sentence with above assigned variables within string +puts "I said: #{x}." +# print out sentence with above assigned variables within string +puts "I also said: '#{y}'." +# assign boolean value +hilarious = false +# assign string with boolean variable within +joke_evaluation = "Isn't that joke so funny?! #{hilarious}" +# print out above assigned variable with boolean in string +puts joke_evaluation +# assign string to variable +w = "This is the left side of..." +# assign string to variable +e = "a string with a right side." +# print out combined variables +puts w + e + + +# Study Drills + +# 2. & 3. I only see four places, the other places where there is a data type inside a string the data type is either an integer or a boolean. +# 4. Combining the two variables "w" and "e" create on longer string because we haven't specified where the combined string should end, therefore they are put together. +# 5. One can use single quotes versus double quotes, but in this instance because we have words in our strings that are contractions (and aren't assigned to a contraction by a string like the "do_not = don't" example) then putting a single quote prematurely ends our string. We could change the word on line 22 "Isn't" to "Is not" and then could use single quotes. diff --git a/day_1/ex7.rb b/day_1/ex7.rb new file mode 100644 index 000000000..5b1fb2f3a --- /dev/null +++ b/day_1/ex7.rb @@ -0,0 +1,22 @@ +print "How old are you? " +age = gets.chomp +print "How tall are you? " +height = gets.chomp +print "How much do you weigh? " +weight = gets.chomp + +puts "So, you're #{age} old, #{height} tall and #{weight} heavy." + +print "Where do you live? " +home = gets.chomp +print "How long have you lived there? " +time = gets.chomp + +puts "You've lived in #{home} for #{time}, wow that's great!" + +# I was very confused on where "gets.chomp" got it's input from. I didn't +# understand how the program knew what age, height or weight I was. After +# googling for two days I finally reached out to a peer and was able to find +# out the information I was missing; I needed to answer the questions being +# posed in my terminal. Genius! This one tripped me up but I learned a lot of +# great lessons from it. diff --git a/day_1/exercises/interpolation.rb b/day_1/exercises/interpolation.rb index c7f4f47df..8f418c740 100644 --- a/day_1/exercises/interpolation.rb +++ b/day_1/exercises/interpolation.rb @@ -15,11 +15,11 @@ speedy = "quick red fox" slow_poke = "lazy brown dog" -p # YOUR CODE HERE +p "The #{speedy} jumped over the #{slow_poke}" # Write code that uses the variables below to form a string that reads # "In a predictable result, the tortoise beat the hare!": slow_poke = "tortoise" speedy = "hare" -# YOUR CODE HERE +p "In a predicatable result, the #{slow_poke} beat the #{speedy}!" diff --git a/day_1/exercises/loops.rb b/day_1/exercises/loops.rb index 90dc15ab1..23eba4390 100644 --- a/day_1/exercises/loops.rb +++ b/day_1/exercises/loops.rb @@ -5,14 +5,16 @@ # Example: Write code that prints your name five times: 5.times do - p "Hermione Granger" + p "Leigh Cepriano Pulzone" end # Write code that prints the sum of 2 plus 2 seven times: 7.times do - # YOUR CODE HERE + p 2 + 2 end # Write code that prints the phrase 'She sells seashells down by the seashore' # ten times: -# YOUR CODE HERE +10.times do + p "She sells seashells down by the seashore" +end diff --git a/day_1/exercises/numbers.rb b/day_1/exercises/numbers.rb index 9a5468a31..45f41377d 100644 --- a/day_1/exercises/numbers.rb +++ b/day_1/exercises/numbers.rb @@ -4,13 +4,13 @@ # `ruby day_1/exercises/numbers.rb` # Example: Write code that prints the result of the sum of 2 and 2: -p 2 + 2 +puts "What is 2 + 2? #{2 + 2}" # Write code that prints the result of 7 subtracted from 83: -p #YOUR CODE HERE +puts "What is 7 subtracted from 83? #{7 - 83}" # Write code that prints the result of 6 multiplied by 53: -# YOUR CODE HERE +puts "What is 6 times 53? #{6 * 53}" # Write code that prints the result of the modulo of 10 into 54: -# YOUR CODE HERE +puts "What is the modulo of 10 divided by 54? #{10 % 54}" diff --git a/day_1/exercises/strings.rb b/day_1/exercises/strings.rb index f2f903ffc..3a01695d1 100644 --- a/day_1/exercises/strings.rb +++ b/day_1/exercises/strings.rb @@ -4,10 +4,21 @@ # `ruby day_1/exercises/strings.rb` # Example: Write code that prints your name to the terminal: -p "Alan Turing" +name = "Leigh Cepriano Pulzone" +puts name # Write code that prints `Welcome to Turing!` to the terminal: -p #YOUR CODE HERE +greeting = "Welcome to Turing!" +puts greeting # Write code that prints `99 bottles of pop on the wall...` to the terminal: -# YOUR CODE HERE +little_diddy = "99 bottles of pop on the wall..." +puts little_diddy + +# I played around with "p", "print" and "puts" here and was very interested in +# what I saw in my terminal! I will be digging deeper into the difference between +# all of the options to find what different situations will call for them as I'm +# sure there is a reason why all three do the same thing with slight differences. +# At first I had the strings put into sentences that would print out such as +# "My name is #{name}." But I then changed it to print out just my name as the +# directions specified, I will happily go back and change it if need be! diff --git a/day_1/exercises/variables.rb b/day_1/exercises/variables.rb index a1e45bb26..5ea57b945 100644 --- a/day_1/exercises/variables.rb +++ b/day_1/exercises/variables.rb @@ -1,29 +1,38 @@ # In the below exercises, write code that achieves # the desired result. To check your work, run this -# file by entering the following command in your terminal: +# file by entering the following command in your terminal: # `ruby day_1/exercises/variables.rb` # Example: Write code that saves your name to a variable and # prints what that variable holds to the terminal: -name = "Harry Potter" +name = "Leigh Cepriano Pulzone" p name # Write code that saves the string 'Dobby' to a variable and # prints what that variable holds to the terminal: -house_elf = "Dobby" -# YOUR CODE HERE +free_house_elf = "Dobby" +p free_house_elf # Write code that saves the string 'Harry Potter must not return to Hogwarts!' # and prints what that variable holds to the terminal: -# YOUR CODE HERE +chamber_of_secrets = "Harry Potter must not return to Hogwarts!" +p chamber_of_secrets # Write code that adds 2 to the `students` variable and # prints the result: students = 22 -# YOUR CODE HERE +students = 22 + 2 p students + # Write code that subracts 2 from the `students` variable and # prints the result: -# YOUR CODE HERE +students = 22 - 2 p students + +# I was unsure if I was being asked to reassign the variable 'students' by typing +# students = 22 + 2 or if I was supposed to create a new variable with the +# variable 'students' in it. +# Also, it was not until I got to the interpolation exercise that I realized the +# first line was just an example which is why you will see all the examples changed. +# What another great lesson learned, read the fine print (maybe a few times...)! diff --git a/day_1/need_to_calculate.rb b/day_1/need_to_calculate.rb new file mode 100644 index 000000000..057d2d034 --- /dev/null +++ b/day_1/need_to_calculate.rb @@ -0,0 +1,8 @@ +puts "How many live shrimp are in the bowl?" + +puts "Let's find out." + + +puts "Alive #{2 + 4 + 6 + 8 / 2}" +puts "Dead #{3 + 5 - 2 * 2}" +puts "There is more alive than dead. #{16 > 4}" diff --git a/day_1/numbers_ruby_in_100_minutes.rb b/day_1/numbers_ruby_in_100_minutes.rb new file mode 100644 index 000000000..9687c69de --- /dev/null +++ b/day_1/numbers_ruby_in_100_minutes.rb @@ -0,0 +1,3 @@ +5.times do + puts "Hello, World!" +end diff --git a/day_1/questions.md b/day_1/questions.md index 73700e323..d6caa41ce 100644 --- a/day_1/questions.md +++ b/day_1/questions.md @@ -1,17 +1,63 @@ ## Day 1 Questions 1. How would you print the string `"Hello World!"` to the terminal? + * I would print the string `"Hello World"` in the terminal by typing in +``` + p "Hello World" +``` -1. What character is used to indicate comments in a ruby file? -1. Explain the difference between an integer and a float? +2. What character is used to indicate comments in a ruby file? + * The `#` character will indicate comments in a ruby file without printing it. -1. In the space below, create a variable `animal` that holds the string `"zebra"` -1. How would you print the string `"zebra"` using the variable that you created above? +3. Explain the difference between an integer and a float? + * The difference between an integer and a float is that a float has a decimal +point `0.001` is an example of a float. This can also be a float `1.0`. An +integer is a number without a decimal point `23` is an integer, so is `-3`. -1. What is interpolation? Use interpolation to print a sentence using the variable `animal`. -1. What method is used to get input from a user? +4. In the space below, create a variable `animal` that holds the string `"zebra"` +``` +animal = "zebra" +``` -1. Name and describe two common string methods: + +5. How would you print the string `"zebra"` using the variable that you created above? + * `p animal` + + +6. What is interpolation? Use interpolation to print a sentence using the variable `animal`. + * Interpolation is putting strings together, I think about it like a collage of strings. +``` +example: +animal = "zebra" +p "I want to ride a #{animal} someday." +"I want to ride a zebra someday." +``` + + +7. What method is used to get input from a user? + * The `gets` method is used to get input from a user. + + +8. Name and describe two common string methods: + * `length` is a method that is used to count the amount of characters in a string + ``` + Example: + name = "Leigh" + p name.length + 5 + ``` + + * `times` is a method that is used to repeat the times a string is printed by the amount put in before. + +``` + Example: + 3.times do + p "Hello" + end + "Hello" + "Hello" + "Hello" +``` diff --git a/day_2/array_methods.md b/day_2/array_methods.md new file mode 100644 index 000000000..f967f2bf2 --- /dev/null +++ b/day_2/array_methods.md @@ -0,0 +1,112 @@ +## Array Methods + +#### `sort` method +* This method takes all the elements in the array and prints them in alphabetical or numerical order. +``` +Example 1: +t = ["the", "happy", "fox", "ran"] +t.sort +=> ["fox", "happy", "ran", "the"] +``` +``` +Example 2: +a = ["3", "2", "9", "5"] +a.sort +=> ["2", "3", "5", "9"] +``` + +#### `each` method +* This method prints any symbol or space in between each element in the array. The set up is `variable.each {|x| print x, "___" }` . When I played around with this in my terminal I found that I could substitute any letter for `x` but could not use a number. Whatever is put in between the double quotations will also print including special characters, alphabetical or numerical characters, and spaces. +``` +Example: +a = ["1", "9", "4", "3", "8", "2"] +a.each {|x| print x, " & " } +1 & 9 & 4 & 3 & 8 & 2 & => ["1", "9", "4", "3", "8", "2"] +``` + +#### `join` method +* This method added onto the variable alone will print all the elements in the array together without any brackets or quotations around each element (prints around whole array), `variable.join`. However, when `join` is followed by a command or specific separator it prints that in between each element. `variable.join("-")`. +``` +Example 1: +f = ["How", "do", "you", "do?"] +f.join +=> "Howdoyoudo?" +``` +``` +Example 2: +f = ["How", "do", "you", "do?"] +f.join("_") +=> "How_do_you_do?" +``` + + +#### `index` method +* This method returns the numerical position that the element is placed in the array, `variable.index("x")`. I liken this to the index in the back of a book. +``` +Example: +n = ["amber", "leigh", "cepriano", "pulzone"] +n.index("leigh") +=> 1 +``` + + +#### `include` method +* This method tells you if an element is contained in the array, `variable.include?("x")`. This method is using boolean logic; I also found that the `?` is necessary as the method will not work without it. +``` +Example: +a = ["1", "9", "4", "3", "8", "2"] +a.include?("2") +=> true +a.include?("5") +=> false +``` + + +#### `collect` method +* This method will add any character within the double quotations after each element in the array, `variable.collect { |x| x + "-" }`. I found this method to be fascinating as well, `variable.map.with_index{ |x, i| x * i }`, this method prints the elements in the array a certain amount of times depending on their position in the array. +``` +Example 1: +t = ["0", "1", "2", "3", "4"] +t.collect { |x| x + "$" } +=> ["0$", "1$", "2$", "3$", "4$"] +``` +``` +Example 2: +t = ["0", "1", "2", "3", "4"] +t.map.with_index{ |x, i| x * i } +=> ["", "1", "22", "333", "4444"] +```` + + +#### `first` method +* This method returns the first element listed in the array, `variable.first`. +``` +Example: +n = ["amber", "leigh", "cepriano", "pulzone"] +n.first +=> "amber" +``` + +#### `last` method +* This method does the opposite of the above mentioned method; this prints the last element in an array, `variable.last`. +``` +Example: +n = ["amber", "leigh", "cepriano", "pulzone"] +n.last +=> "pulzone" +``` + +#### `shuffle` method +* This method is a fun one, it will scramble the array, `variable.shuffle`. There is also a method that will shuffle the array but allow the user to call upon that same randomly scrambled output, `variable.shuffle(random: Random.new(x))`. +``` +Example 1: +g = ["this", "will", "get", "weird"] +g.shuffle +=> ["get", "weird", "this", "will"] +``` +``` +Example 2: +g = ["this", "will", "get", "weird"] +g.shuffle(random: Random.new(3)) +=> ["weird", "will", "this", "get"] +``` diff --git a/day_2/exercises/arrays.rb b/day_2/exercises/arrays.rb index f572a5ae6..563c004aa 100644 --- a/day_2/exercises/arrays.rb +++ b/day_2/exercises/arrays.rb @@ -10,31 +10,35 @@ # Write code that stores an array of states in a variable, # then prints that array: -states = #YOUR CODE HERE +states = ["Washington", "Oregon", "Idaho", "California", "Colorado"] p states # Write code that stores an array of foods in a variable, # then prints that array: -# YOUR CODE HERE +foods = ["Bagel", "Bacon", "Avocado", "Cream Cheese"] +p foods # Example: Write code that prints the number of elements # in your above array of animals: +p states.count p animals.count # Write code that prints the number of elements # in your above array of foods: -# YOUR CODE HERE +p foods.count # Write code that prints "Zebra" from your animals array: -# YOUR CODE HERE +p animals.first # Write code that prints the last item of your foods array: -# YOUR CODE HERE +p foods.last # Write code that adds "lion" to your animals array # and prints the result (Hint- use a method): -# YOUR CODE HERE +animals.push "Lion" +p animals # Write code that removes the last element from your foods array # and prints the result (Hint- use a method): -# YOUR CODE HERE +foods.pop +p foods diff --git a/day_2/exercises/boolean_practice.rb b/day_2/exercises/boolean_practice.rb new file mode 100644 index 000000000..b0d5267dd --- /dev/null +++ b/day_2/exercises/boolean_practice.rb @@ -0,0 +1,75 @@ +true && true +=> true + +false && true +=> false + +1 == 1 && 2 == 1 +=> false + +"test" == "test" +=> true + +1 == 1 || 2 != 1 +=> true + +true && 1 == 1 +=> true + +false && 0 != 0 +=> true # This is incorrect, false is the correct answer. I was unsure if two +# falses made a true or a false. I read this as 'false and 0 not equal 0' +# which is false. Oh! I just got it, haha. + +true || 1 == 1 +=> true + +"test" == "testing" +=> false + +1 != 0 && 2 == 1 +=> false + +"test" != "testing" +=> true + +"test" == 1 +=> false # Initially I forgot to put double quotations around the word 'test', +# when I typed this into my terminal to check my work I got an error message +# and that's how I saw my mistake. + +!(true && false) +=> true + +!(1 == 1 && 0 != 1) +=>false + +!(10 == 1 || 1000 == 1000) +=> true # False is the correct answer. I looked at the question as it's true +# that 10 being equal to 1 is NOT true. I'm starting to see that if it's not +# true in the equation then it's false. + +!(1 != 10 || 3 == 4) +=> true # False is the correct answer. The '!' in the beginning is tripping me +# up a little. Taking a step back and looking at it logically is something I +# will practice more of with these. 1 != 10 is true, 3 == 4 is false the '!' +# outside of the () is 'not'. This would make the inside false, it's slowly +# starting to sink in... + +!("testing" == "testing" && "Zed" == "Cool Guy") +=> false # True is the correct answer. I was taking a stab in the dark with my +# answer as I wasn't sure how the computer would react to "Zed" and "Cool Guy". + +1 == 1 && (!("testing" == 1 || 1 == 0)) +=> true + +"chunky" == "bacon" && (!(3 == 4 || 3 == 3)) +=> false + +3 == 3 && (!("testing" == "testing" || "Ruby" == "Fun")) +=> false + +# This was a very interesting exercise! I typed in quite a lot of different +# combinations in my terminal to figure out things like "Zed" == "Cool Guy" +# and what I could do to make it true. I also found that "Zed" == "zed" is false. +# It was very interesting to see what the computer would put as true or false. diff --git a/day_2/exercises/iteration.rb b/day_2/exercises/iteration.rb index a801cb4fc..8be886bd9 100644 --- a/day_2/exercises/iteration.rb +++ b/day_2/exercises/iteration.rb @@ -15,14 +15,22 @@ # "The is awesome!" for each animal: animals.each do |animal| - # YOUR CODE HERE + p "The #{animal} is awesome!" end -# Write code that stores an array of foods in a variable, +# Write code that stores an array of foods in a variable, # then iterates over that array to print # "Add to shopping list" for each food item: -# YOUR CODE HERE +foods = ["Blueberries", "Sharp Cheddar", "Milk", "Eggs", "Ravioli"] -# Write code that stores an array of numbers in a variable, -# then iterates over that array to print doubles of each number: -# YOUR CODE HERE +foods.each do |f| + p "Add #{f} to shopping list" +end + +# Write code that stores an array of numbers in a variable, +# then iterates over that array to print doubles of each number: +numbers = ["0", "1", "2", "3", "4", "5"] + +numbers.each do |n| + p n *2 +end diff --git a/day_2/exercises/iteration_and_each.rb b/day_2/exercises/iteration_and_each.rb new file mode 100644 index 000000000..5f241e0fc --- /dev/null +++ b/day_2/exercises/iteration_and_each.rb @@ -0,0 +1,83 @@ +numbers = [1, 2, 3, 4, 5, 6] + +numbers.each do |number| + puts number + puts number +end + +numbers.each do |number| + puts number + puts number + puts number +end +# Had trouble getting the desired result with in the block, I know there has to +# be an easier way but could not find it. + +numbers.each do |n| + puts n if n % 2 == 0 +end + +numbers.each do |n| + puts n if n % 2 == 1 +end +# Reached out to a peer with help on this, they were able to teach me rather +# than just tell me the answer. Realized there is a gap in my foundational +# math skills that I will be brushing up on. + +num = [1, 2, 3, 4, 5, 6] + +num.each do |n| + puts n * 2 +end +# I was able to get the result but feel as though there is a neater way to +# put all of this together. + +names = ["Alice Smith", "Bob Evans", "Roy Rogers"] + +names.each do |name| + puts name +end + +names.each do |name| + puts name.split.first +end + +names.each do |name| + puts name.split.last +end +# Had an "ah-ha!" moment when I realzied that I could add methods to the array +# name on the 'puts' line. Oh the things to learn! + +names.each do |name| + puts name.split.first[0] + puts name.split.last[0] +end + +#Woohoo! Able to finally get the initials!! + +names.each do |name| + puts name.split.first[0] + "." + name.split.last[0] + "." +end + +names.each do |name| + puts name.split.first[0] + ". " + name.split.last +end +# I was finally able to get this to work after asking a peer for help! + +names.each do |name| + puts name.length +end +# I was not sure this is exercise was asking for the total number of characters +# for the entire array or for each string inside the array. + +# Overall this exercise taught me that I have a lot more digging into arrays +# and methods. After not being able to figure out a few of these methods +# I moved onto the rest of the day_2 exercises; that helped a little +# bit as I brought some of what I learned back to this lesson. +# I have decided to move onto day_3 at this point because I have spent two days +# working on this day now. +# My plan is to push this to GitHub and revisit this at a later time. +# I have put these notes in here in case I'm not able to get back to it. +# I wanted to ensure that the reviewer of this knew that I put a +# considerable amount of effort into understanding the +# concepts and will be revisiting it at a later date. diff --git a/day_2/questions.md b/day_2/questions.md index a179f0b04..becb2c57b 100644 --- a/day_2/questions.md +++ b/day_2/questions.md @@ -1,17 +1,61 @@ ## Day 2 Questions 1. Create an array containing the following strings: `"zebra", "giraffe", "elephant"`. + ``` + ["zebra", "giraffe", "elephant"] + ``` 1. Save the array you created above to a variable `animals`. + ``` + animals = ["zebra", "giraffe", "elephant"] + ``` 1. Using the array `animals`, how would you access `"giraffe"`? + ``` + animals.fetch(1) + =>"giraffe" + ``` 1. How would you add `"lion"` to the `animals` array? + ``` + animals.push "lion" + ``` 1. Name and describe two additional array methods: + One of my favorite methods I learned about was the `shuffle` method. When this method is called it shuffles the elements in the array. + ``` + Example: + j = ["this", "will", "be", "fun"] + j.shuffle + => ["fun", "this", "will", "be"] + ``` + Another way to call this method is by using `shuffle(random: Random.new(x))`. You can put in any number for `x` and it will shuffle the array but allow the user to call upon that shuffle again. + ``` + Example: + j = ["this", "will", "be", "fun"] + j.shuffle(random: Random.new(13)) + => ["will", "fun", "this", "be"] + ``` + There is also the `include` method which uses boolean logic to tell the user if the asked for element is present in the array. + ``` + Example: + j = ["this", "will", "be", "fun"] + j.include?("fun") + => true + ``` + 1. What are the boolean values in Ruby? + * Boolean values in Ruby are `true` or `false` values. 1. In Ruby, how would you evaluate if `2` is equal to `25`? What is the result of this evaluation? +``` +2 == 25 +=> false +``` 1. In Ruby, how would you evaluate if `25` is greater than `2`? What is the result of this evaluation? +``` +25 >= 2 +=> true +``` diff --git a/day_3/exercises/conditionals.rb b/day_3/exercises/conditionals.rb new file mode 100644 index 000000000..326a009f1 --- /dev/null +++ b/day_3/exercises/conditionals.rb @@ -0,0 +1,17 @@ +def water_status(minutes) + if minutes < 7 + puts "The water is not boiling yet." + elsif minutes == 7 + puts "It's just barely boiling." + elsif minutes == 8 + puts "It's boiling!" + else + puts "Hot! Hot! Hot!" + end +end + +# I ran this program initially in my terminal and was able to get the correct +# output. The good lesson for me was learning that there can be multiple +# 'elsif' statements in one block. It was also interesting to see that when +# I type it out in here it doesn't run the same way when I call upon this +# in my terminal. diff --git a/day_3/exercises/else_and_if.rb b/day_3/exercises/else_and_if.rb new file mode 100644 index 000000000..587ed4ce7 --- /dev/null +++ b/day_3/exercises/else_and_if.rb @@ -0,0 +1,82 @@ +# Lines 2 - 4 are assigning integers to variables. +people = 30 +cars = 40 +trucks = 15 + +# if cars are greater than people +if cars > people +# print this line if the above is true + puts "We should take the cars." +# OR if there are less cars than people print the line below +elsif cars < people +# print this line if the above is true + puts "We should not take the cars." +# if neither of the two top choices are true +else +# print this line if the above is true + puts "We can't decide." +# end of the block +end + +# if trucks are greater than cars print the line below +if trucks > cars +# print if the above is true + puts "That's too many trucks." +# OR if there are less trucks then cars print the line below +elsif trucks < cars +# print this if the above line is true + puts "Maybe we could take the trucks." +# if neither are true print this line +else +# print this line + puts "We still can't decide." +# end of the block +end + +# if people are greater than trucks print the line below +if people > trucks +# print this if the above line is true + puts "Alright, let's just take the trucks." +# if the above statement isn't true print the line below +else +# print this if the above is true + puts "Fine, let's stay home then." +# end of the block +end + +# 1. The 'elsif' statement seems to be saying something to the affect of 'or' +# as in this or that. The 'else' statement is there in case neither 'if' or +# 'elsif' is true. When I changed the value of 'cars' to 30 then the 'else' +# statement of "We can't decide." printed out. + +# if there are more cars than people or less trucks than cars print the line +# below +if cars > people || trucks < cars + puts "Let's just walk." +end + +# if people is not equal to trucks AND people is less than equal to cars print +# the line below +if people != trucks && people <= cars + puts "I've got it! We'll fly on our broomsticks, then." +# or if people is equal to trucks OR people is equal to cars print the line +# below +elsif people == trucks || people == cars + puts "Never mind, we aren't going anywhere." +# if neither are true print the line below +else + puts "We'll ride the train." +end + +# if trucks are less than equal to cars AND people are not equal to trucks +# print the line below +if trucks <= cars && people != trucks + puts "Wait, where are we going in the first place?" +# or if trucks are equal to people OR people are greater than equal to cars +# print the line below +elsif trucks == people || people >= cars + puts "We're going to be late!" +# if neither are true print the line below +else + puts "Why the rush?" +end diff --git a/day_3/exercises/if_statements.rb b/day_3/exercises/if_statements.rb index a80b96840..c83e85eb2 100644 --- a/day_3/exercises/if_statements.rb +++ b/day_3/exercises/if_statements.rb @@ -3,14 +3,16 @@ # file by entering the following command in your terminal: # `ruby day_3/exercises/if_statements.rb` -# Example: Using the weather variable below, write code that decides +# Example: Using the weather variable below, write code that decides # what you should take with you based on the following conditions: # if it is sunny, print "sunscreen" # if it is rainy, print "umbrella" # if it is snowy, print "coat" # if it is icy, print "yak traks" - weather = 'snowy' + # weather = 'snowy' + # weather = 'sunny' + weather = 'cloudy' if weather == 'sunny' p "sunscreen" @@ -35,21 +37,26 @@ # Right now, the program will print # out both "I have enough money for a gumball" and -# "I don't have enough money for a gumball". Write a +# "I don't have enough money for a gumball". Write a # conditional statement that prints only one or the other. # Experiment with manipulating the value held within num_quarters # to make sure both conditions can be achieved. -num_quarters = 0 +# num_quarters = 0 +num_quarters = 1 + +if num_quarters > 0 + puts "I have enough money for a gumball" +elsif num_quarters == 0 + puts "I don't have enough money for a gumball" +end -puts "I have enough money for a gumball" -puts "I don't have enough money for a gumball" ##################### # Using the variables defined below, write code that will tell you -# if you have the ingredients to make a pizza. A pizza requires +# if you have the ingredients to make a pizza. A pizza requires # at least two cups of flour and sauce. # You should be able to change the variables to achieve the following outputs: @@ -61,5 +68,25 @@ # Experiment with manipulating the value held within both variables # to make sure all above conditions output what you expect. -cups_of_flour = 1 +# cups_of_flour = 1 +# has_sauce = true +# +# cups_of_flour = 5 +# has_sauce = false +# +# cups_of_flour = 2 +# has_sauce = true + +cups_of_flour = 3 has_sauce = true + + +if cups_of_flour >= 2 && has_sauce == true + puts "I can make pizza" +elsif cups_of_flour <= 2 && has_sauce == false + puts "I cannot make pizza" +elsif cups_of_flour >= 2 && has_sauce == false + puts "I cannot make pizza" +elsif cups_of_flour <= 2 && has_sauce == true + puts "I cannot make pizza" +end diff --git a/day_3/exercises/making_decisions.rb b/day_3/exercises/making_decisions.rb new file mode 100644 index 000000000..92a0cc999 --- /dev/null +++ b/day_3/exercises/making_decisions.rb @@ -0,0 +1,76 @@ +puts "You enter a dark room with two doors. Do you go through door #1 or door #2?" + +print "> " +door = $stdin.gets.chomp + +if door == "1" + puts "There's a giant bear here eating a cheese cake. What do you do?" + puts "1. Take the cake." + puts "2. Sing to the bear." + + print "> " + bear = $stdin.gets.chomp + + if bear == "1" + puts "The bear cries and runs away. Good job!" + elsif bear == "2" + puts "The bear sings along. Good job!" + else + puts "Well, doing %s is probably better. Bear runs away." % bear + end + +elsif door == "2" + puts "You stare into the endless abyss at Cthulhu's retina." + puts "1. Blueberries." + puts "2. Yellow jacket clothespins." + puts "3. Understanding revolvers yelling melodies." + + print "> " + insanity = $stdin.gets.chomp + + if insanity == "1" || insanity == "2" + puts "Your body survives powered by a mind of jello. Good job!" + else + puts "The insanity rots your eyes into a pool of muck. Good job!" + end +# I didn't know how to get this answer for a while, I finally figured it out! +else + puts "You stumble around and fall on a knife and die. Good job!" +end + +# My game: +puts "You can choose staircase #7 or staircase #13, which one do you choose?" + +print "> " +staircase = $stdin.gets.chomp + +if staircase == "7" + puts "The staircase is an escalator, how do you ride it?" + puts "1. Easy-like. Let the stairs carry you." + puts "2. It's a race! Run to the top as fast as you can." + + print "> " + escalator = $stdin.gets.chomp + + if escalator == "1" + puts "Patience pays off, you are on your way to a cloud full of puppies. Yay!" + elsif escalator == "2" + puts "You are running your way to an angry cloud of lightening! Oh dear!" + end + +elsif staircase == "13" + puts "You've reached another choice! Which way?" + puts "1. Up?" + puts "2. Down?" + + print "> " + choice = $stdin.gets.chomp + + if choice == "1" + puts "You reach the top and fall for eternity. Bummer buddy." + elsif choice == "2" + puts "You are stuck in M.C. Escher's infinite staircase. Bummer buddy." + end +end + +# Oh my goodness. I had a blast with this! I would have to reference this again to be able to recreate it. But I thoroughly enjoyed the process! diff --git a/day_3/exercises/what_if.rb b/day_3/exercises/what_if.rb new file mode 100644 index 000000000..39fee5be5 --- /dev/null +++ b/day_3/exercises/what_if.rb @@ -0,0 +1,76 @@ +people = 20 +cats = 30 +dogs = 15 + + +if people < cats + puts "Too many cats! The world is doomed!" +end + +if people > cats + puts "Not many cats! The world is saved!" +end + +if people < dogs + puts "The world is drooled on!" +end + +if people > dogs + puts "The world is dry!" +end + + +dogs += 5 + +if people >= dogs + puts "People are greater than or equal to dogs." +end + +if people <= dogs + puts "People are less than or equal to dogs." +end + + +if people == dogs + puts "People are dogs." +end + + +# The 'if' statement seems to pose a question that needs to be answered, I +# want to say it's using boolean logic because it's seeking a true or false +# answer in order to answer the question being asked. + +# 1. I think the 'if' acts as 'do' or 'def' for the code beneath it as in +# it starts a block. + +# 2. I'm not sure why the code beneath the 'if' needs to be indented by two +# spaces but I would guess it has to do with the indented code knowing that +# it's part of that specific block? Kind of like the peanut butter and jelly +# on a pbj sandwich with crust still on, you don't want the pbj to leak out +# of the sides of the sandwich. It would look messy that way. Not sure if +# that's really correct but that's how I see it. + +# 3. Hmm..interesting. Nothing different happens when the the 'puts' line +# isn't indented. I would assume my above answer is correct, that it looks +# neat and tidy. (No jelly on the floor here!) + +burritos = 10 +tomatoes = 5 + +if tomatoes != burritos + puts "Yay! I can eat my burrito!" +end + +tomatoes += 5 + +if tomatoes == burritos + puts "I guess I will I go hungry." +end + +# 5. When I change the initial values for each variable it changes whether or +# not the 'if-statements' are true or false, thus effecting the print out +# of the above statements. + + +# I would like to state that after finishing this lesson I immediately went on +# to the next lesson which answers all of the above asked questions. diff --git a/day_3/questions.md b/day_3/questions.md index db6170fa7..aa38d28b1 100644 --- a/day_3/questions.md +++ b/day_3/questions.md @@ -1,13 +1,31 @@ ## Day 3 Questions 1. What is a conditional statement? Give three examples. - +``` +if +elsif +else +``` 1. Why might you want to use an if-statement? + * You might want to use an `if` statement to create choice for the user. This would allow for different options to be in place and would give provide different outcomes depending on the circumstances. 1. What is the Ruby syntax for an if statement? + * This seems like a simple answer which makes me wary...but I would put `if` followed by the conditions. 1. How do you add multiple conditions to an if statement? + * To add multiple conditions to an initial `if` statement you would use `elsif` and `else` to add more conditions. To complete the block you would put `end` when finished listing the conditions. 1. Provide an example of the Ruby syntax for an if/elsif/else statement: +``` +Example: + if dogs <= 4 + puts "That's not enough!" + elsif dogs == 4 + puts "That's a perfect amount of dogs!" + else + puts "Wow, that's a lot of dogs!" + end +``` 1. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? + * One way that I saw was using `unless` instead of `if` this would look at the condition as in "If this isn't true, do this..." diff --git a/day_4/exercises/intro_into_methods.rb b/day_4/exercises/intro_into_methods.rb new file mode 100644 index 000000000..968c759ee --- /dev/null +++ b/day_4/exercises/intro_into_methods.rb @@ -0,0 +1,45 @@ +# Functions do three things: +# 1. They name pieces of code the way variables name strings and numbers. +# 2. They take arguments the way your scripts take ARGV. +# 3. Using 1 and 2, they let you make your own "mini-scripts" or "tiny commands." + +# this one is like your scrips with ARGV +def print_two(*args) + arg1, arg2, = args + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +# ok, that *args is actually pointless, we can just do this +def print_two_again(arg1, arg2) + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +# this just takes one argument +def print_one(arg1) + puts "arg1: #{arg1}" +end + +# this one takes no arguments +def print_none() + puts "I got nothin'." +end + + +print_two("Zed", "Shaw") +print_two_again("Zed", "Shaw") +print_one("First!") +print_none() + +def add_numbers(num1, num2) + result = num1 + num2 + puts result +end + +add_numbers(4, 7) + +def plus(n1, n2, n3, n4, n5) + result = n1 + n2 + n3 + n4 + n5 + puts result +end + +plus(2, 3, 45, 5, 2) diff --git a/day_4/exercises/methods.rb b/day_4/exercises/methods.rb index 6ed338e5d..bb4a8b4b3 100644 --- a/day_4/exercises/methods.rb +++ b/day_4/exercises/methods.rb @@ -12,16 +12,45 @@ def print_name # Write a method that takes a name as an argument and prints it: def print_name(name) - # YOUR CODE HERE -end + puts "My name is #{name}." # Not sure if I needed to just print the name +end # or if I needed the string as well. print_name("Albus Dumbledore") -# Write a method that takes in 2 numbers as arguments and prints +# Write a method that takes in 2 numbers as arguments and prints # their sum. Then call your method: -# YOUR CODE HERE +def numbers(x, y) + puts "#{x + y}" +end + +x = 10 +y = 33 + +numbers(x, y) -# Write a method that takes in two strings as arguments and prints -# a concatenation of those two strings. Example: The arguments could be -# (man, woman) and the end result might output: "When Harry Met Sally". +puts "I could do this..." +puts "Here is my result: #{x + y}" + + +# Write a method that takes in two strings as arguments and prints +# a concatenation of those two strings. Example: The arguments could be +# (man, woman) and the end result might output: "When Harry Met Sally". # Then call your method: +def ryhme(boy, girl) + puts "#{boy} and #{girl} went up the hill to fetch a pail of water." + puts "#{boy} fell down and broke his crown, and #{girl} came tumbling after." +end + +boy = "Sal" +girl = "Gertie" + +ryhme(boy, girl) + +def karma(a, b) + puts "What goes #{a}, must come #{b}." +end + +karma(a = "up", b = "down") + +# I liked ths exercise and wanted to play around a bit more, that's why I +# wrote two. diff --git a/day_4/exercises/methods_and_return_values.rb b/day_4/exercises/methods_and_return_values.rb new file mode 100644 index 000000000..716824f4d --- /dev/null +++ b/day_4/exercises/methods_and_return_values.rb @@ -0,0 +1,59 @@ +def add(a, b) + puts "ADDING #{a} + #{b}" + return a + b +end + +def subtract(a, b) + puts "SUBTRACTING #{a} - #{b}" + return a - b +end + +def multiply(a, b) + puts "MULTIPLYING #{a} * #{b}" + return a * b +end + +def divide(a, b) + puts "DIVIDING #{a} / #{b}" + return a / b +end + + +puts "Let's do some math with just functions!" + +age = add(30, 5) +height = subtract(78, 4) +weight = multiply(90, 2) +iq = divide(100, 2) + +puts "Age: #{age}, Height: #{height}, Weight: #{weight}, IQ: #{iq}" + + +# # A puzzle for the extra credit, type it in anyway. +puts "Here is a puzzle." + +what = add(age, subtract(height, multiply(weight, divide(iq, 2)))) +# 50(iq) / 2 = 25 +# 180(weight) * 25 = 4500 +# 74(height) - 4500 = -4426 This is where I went wrong, I did 4500 - 74 on paper +# 35(age) + -4426 = -4391 + +puts "That becomes: #{what}. Can you do it by hand?" +# I tried this by hand and got (most) of it correct. Looks like I really need +# to brush up on basic math skills! Good to know! + +def plant(x, y) + puts "This plant has #{x} living leaves and #{y} dead leaves." + return x + y +end + +def tree(x, y) + puts "This tree has a trunk that's #{x} feet tall and #{y} feet wide." +return x * y +end + +zeezee = plant(30, 0) +redwood = tree(50, 10) + +puts "I saw a ZeeZee plant that only had #{zeezee} leaves on it, poor thing!" +puts "Well I saw a tree that was #{redwood} feet big! Talk about miracle grow!" diff --git a/day_4/exercises/methods_and_variables.rb b/day_4/exercises/methods_and_variables.rb new file mode 100644 index 000000000..4a52c5ea3 --- /dev/null +++ b/day_4/exercises/methods_and_variables.rb @@ -0,0 +1,52 @@ +# This starts the function by using 'def' and naming the function +# (cheese_and_crackers) and naming the arguments (cheese_count & boxes_of_crackers) +def cheese_and_crackers(cheese_count, boxes_of_crackers) +# The next two lines use the arguments to tell the computer what to put in place +# this is signified with the #{}. + puts "You have #{cheese_count} cheeses!" + puts "You have #{boxes_of_crackers} boxes of crackers!" +# This will print the string. + puts "Man that's enough for a party!" +# This line takes the sum of the two arguments together and prints them in the +# string. + puts "Wow, #{cheese_count + boxes_of_crackers} cheeses and crackers is a lot of food!" +# This prints the string below, I am unsure of what the \n does. I have taken it +# out and gotten the same result as well as replacing the n with another letter. +# I will be looking into that. + puts "Get a blanket.\n" +# This ends the function. +end + +# Prints the string below followed by the strings in the function above. +puts "We can just give the function numbers directly:" +# These numbers are put in place for the arguments in the function. +cheese_and_crackers(20, 30) + +# Prints the string below followed by the strings in the function above. +puts "OR, we can use variables from our script:" +# Assigning amounts for the arguments, these numbers will be printed out in +# the function. +amount_of_cheese = 10 +amount_of_crackers = 50 + +# This line is what calls the function for the "OR, we can.." string. +cheese_and_crackers(amount_of_cheese, amount_of_crackers) + +# Prints the string below above the function. +puts "We can even do math inside too:" +# Calls the function, has the sum of the equations for the arguments. +cheese_and_crackers(10 + 20, 5 + 6) + +# Prints the string below about the function. +puts "And we can combine the two, variables and math:" +# Calls the function and has the sum of the equations for the arguments. +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) + +# Same as above. +puts "So I can write whatever I want here and it will print?" +cheese_and_crackers(4 * 3, 8 - 2) + +def hello(n) + puts "#{n} I am" +end +hello("Sam") diff --git a/day_4/exercises/study_drills_exercise_19.rb b/day_4/exercises/study_drills_exercise_19.rb new file mode 100644 index 000000000..340780034 --- /dev/null +++ b/day_4/exercises/study_drills_exercise_19.rb @@ -0,0 +1,86 @@ +def hawk_the_dog(bones, squeeky_toys) + puts "I have SO many things to chew on!" + puts "See, I have #{bones} bones to chew." + puts "And just look! I have #{squeeky_toys} squeeky toys to destroy!" + puts "At this rate I'll be able to chew and destroy for a year!" + puts "Wait, what's #{bones} plus #{squeeky_toys} divided by 365 in dog years?!" +end + +puts "What fun!" +hawk_the_dog(5, 10) + +puts " " +puts "Just lookie here!" +bones = 70 +squeeky_toys = 100 + +hawk_the_dog(bones, squeeky_toys) + + +puts " " +puts "Oh joy!" +hawk_the_dog(bones + 4 * 4,squeeky_toys + 2 * 9) + + +puts " " +puts "Well, well! This is AMAZING!" +hawk_the_dog(5 * 8 + 100, 9 + 9 * 50) + + +puts " " +puts "I'm the luckiest dog alive!" +hawk_the_dog(bones - 3, squeeky_toys / 2) + + +puts " " +puts "My life is complete!" +bones = 184736 % 19 +squeeky_toys = 4 + 7 + +hawk_the_dog(bones, squeeky_toys) + + +puts " " +puts "I can share my toys!" +puts "Why?!" +puts "Because...." +hawk_the_dog(100, 987) + + +puts " " +bones = "1 million" +squeeky_toys = "2 billion" +puts "I'm rich!" +puts "Why you ask?!" +puts "Because I have #{bones} bones AND #{squeeky_toys} squeeky toys, DUH!" +hawk_the_dog(bones, squeeky_toys) + + +puts " " +bones = 27 +squeeky_toys = 35 +puts "So, I got neutered yesterday and my parents felt so bad and they gave me #{bones + squeeky_toys} toys!" +hawk_the_dog(bones, squeeky_toys) + + +puts " " +puts "So this is just the BEST DAY EVER!!" +puts "Want to hear why? YES or NO?" + +print "> " +why = $stdin.gets.chomp + +if why == "YES" + puts "Because of all the chewing possibilities!" +elsif why == "NO" + puts "Well, fine then. But I'll tell you anyway!" +else + puts "SQUIRREL!!!!" +end + +puts "LOOK LOOK LOOK!!" +hawk_the_dog(100, 75) + +# I had a lot of fun with this exercise. I learned a lot with the last three +# examples I put. The very last one I thought I failed on until I remembered +# that I need to put my arguments after my function in order to call it! diff --git a/day_4/exercises/what_are_methods.rb b/day_4/exercises/what_are_methods.rb new file mode 100644 index 000000000..57124c9c9 --- /dev/null +++ b/day_4/exercises/what_are_methods.rb @@ -0,0 +1,42 @@ +puts "hello" +puts "hi" +puts "how are you" +puts "I'm fine" + +def say(words) + puts words + '.' +end + +say("hello") +say("hi") +say("how are you") +say("I'm fine") + +def say(words='hello') + puts words + '.' +end + +say() +say("hi") +say("how are you") +say("I'm fine") + +a = 5 + +def some_method + a = 3 +end + +puts a + +# Method invocation with a block + +[1, 2, 3].each do |num| # is the difference here literally the '||'? + puts num +end + +# Method definition + +def print_num(num) + puts num +end diff --git a/day_4/questions.md b/day_4/questions.md index af17ab4da..0b2cdb430 100644 --- a/day_4/questions.md +++ b/day_4/questions.md @@ -1,11 +1,29 @@ ## Day 4 Questions 1. In your own words, what is the purpose of a method? + * A `method` is like a container that takes input and manipulates the input to give output. If it's the same method then it will manipulate the input in the same way. 1. Create a method named `hello` that will print `"Sam I am"`. +``` +def hello + puts "Sam I am" +end +puts hello +Sam I am +``` -1. Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`. +1. Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`. +``` +def hello_someone(name) + puts "#{name} I am" +end +``` 1. How would you call or execute the method that you created above? +``` +hello_someone("Sam") +Sam I am +``` 1. What questions do you have about methods in Ruby? + * I feel like I have a base knowledge of methods in Ruby. I am attending study sessions with my peers in order to grasp the concept better. diff --git a/day_5/exercises/exercise_39.rb b/day_5/exercises/exercise_39.rb new file mode 100644 index 000000000..7a4fc9ea9 --- /dev/null +++ b/day_5/exercises/exercise_39.rb @@ -0,0 +1,103 @@ +# create a mapping of state to abbreviation +states = { + 'Oregon' => 'OR', + 'Flordia' => 'FL', + 'California' => 'CA', + 'New York' => 'NY', + 'Michigan' => 'MI' +} + +# create a basic set of states and some cities in them +cities = { + 'CA' => 'San Francisco', + 'MI' => 'Detroit', + 'FL' => 'Jacksonville' +} + +# add some more cities +cities['NY'] = 'New York' +cities['OR'] = 'Bend' # I live here..I had to change it. + +# puts out some cities +puts '-' * 10 +puts "NY State has: #{cities['NY']}" +puts "OR State has: #{cities['OR']}" + +# puts some states +puts '-' * 10 +puts "Michigan's abbreviation is: #{states['Michigan']}" +puts "Flordia's abbreviation is: #{states['Flordia']}" + +# do it by using the state then cities dict +puts '-' * 10 +puts "Michigan has: #{cities[states['Michigan']]}" +puts "Flordia has: #{cities[states['Flordia']]}" + +# puts every state abbreviation +puts '-' * 10 +states.each do |state, abbrev| + puts "#{state} is abbreviated #{abbrev}" +end + +# puts every city in state +puts '-' * 10 +cities.each do |abbrev, city| + puts "#{abbrev} has the city #{city}" +end + +# not do both at the same time +puts '-' * 10 +states.each do |state, abbrev| + city = cities[abbrev] + puts "#{state} is abbreviated #{abbrev} and has city #{city}" +end + +puts '-' * 10 +# by default ruby says "nil" when something isn't in there +state = states['Texas'] + +if !state + puts "Sorry, no Texas." +end + +# default values using ||= with the nil result +city = cities['TX'] +city ||= 'Does Not Exist' +puts "The city for the state 'TX' is: #{city}" + + +# Study Drills + +home = { + 'California' => 'was born', + 'Washinton' => 'grew up', + 'Oregon' => 'currently live' +} + +town = { + 'CA' => 'Mission Viejo', + 'WA' => 'Tacoma', + 'OR' => 'Bend' +} + +state = { + 'Cal' => 'California', + 'Wash' => 'Washinton', + 'Org' => 'Oregon' +} + +puts '-' * 10 +puts "This is the story of where I have lived." +home.each do |x, y| + puts "#{x} is where I #{y}." +end + +puts '-' * 10 + puts "I didn't live in #{town['CA']} for very long." + puts "I spent most of my childhood in #{town['WA']} and the surrounding areas." + puts "I have lived in #{town['OR']} for almost ten years now." + + puts '-' * 10 + puts "In #{state['Org']} the locals don't like it when people move up from #{state['Cal']}." + puts "They are okay with people who move from #{state['Wash']}, though." + puts "I guess there's a difference between #{state['Cal']} locals and #{state['Wash']} locals here in #{state['Org']}." diff --git a/day_5/exercises/hashes.rb b/day_5/exercises/hashes.rb index 99fcebb77..014179c84 100644 --- a/day_5/exercises/hashes.rb +++ b/day_5/exercises/hashes.rb @@ -1,6 +1,6 @@ # In the below exercises, write code that achieves # the desired result. To check your work, run this -# file by entering the following command in your terminal: +# file by entering the following command in your terminal: # `ruby day_5/exercises/hashes.rb` # Example: Write code that prints a hash holding grocery store inventory: @@ -8,21 +8,39 @@ p foods # Write code that prints a hash holding zoo animal inventory: -zoo = #YOUR CODE HERE +# zoo = { +# 'penguin' => 13, # I wrote my code this way at first because I could not +# 'anteater' => 3, # get my individual value to print out. I looked back at +# 'giraffe' => 6, # the reading material and found that I needed the ':' +# 'sloth' => 4, # in order to call the single value 'penguin'. I also +# 'meerkat' => 35 # found that it doesn't matter if the keys & values are +# } # written on one line or stacked it still works the same. +# p zoo +zoo = {penguin: 13, anteater: 3, giraffe: 6, sloth: 4, meerkat: 35} p zoo -# Write code that prints all of the 'keys' of the zoo variable +# Write code that prints all of the 'keys' of the zoo variable # you created above: -# YOUR CODE HERE +zoo.each do |animal, count| + puts "#{animal}" +end -# Write code that prints all of the 'values' of the zoo variable +# Write code that prints all of the 'values' of the zoo variable # you created above: -# YOUR CODE HERE +zoo.each do |animal, count| + puts "#{count}" +end -# Write code that prints the value of the first animal of the zoo variable +# Write code that prints the value of the first animal of the zoo variable # you created above: -# YOUR CODE HERE + puts "#{zoo[:penguin]}" -# Write code that adds an animal to the zoo hash. +# Write code that adds an animal to the zoo hash. # Then, print the updated hash: -# YOUR CODE HERE +zoo[:tortoise] = 2 + puts zoo + +# Here I am unable to get a ':' to print in front of the +# tortoise key. I tried to put just zoo[tortose:] = 2 +# as well as zoo.tortoise: 2 and was unable to get that consistency. +# Hahaha, I just figured it out!!!! diff --git a/day_5/questions.md b/day_5/questions.md index d059e12c6..438171f11 100644 --- a/day_5/questions.md +++ b/day_5/questions.md @@ -1,13 +1,38 @@ ## Day 5 Questions 1. What is a Hash, and how is it different from an Array? + * A hash is a collection of keys and values, an array would use only numbers to access the information whereas a hash uses names of things to call upon the keys or values. 1. In the space below, create a Hash stored to a variable named `pet_store`. This hash should hold an inventory of items and the number of that item that you might find at a pet store. +``` +Example: +pet_store = { + birds: 30, + cats: 10, + dogs: 15, + fish: 200 +} +``` 1. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? +``` +puts "#{states[:IA]}" +``` 1. With the same hash above, how would we get all the keys? How about all the values? +``` +states.each do |abbrev, full| + puts "#{abbrev}" +end +``` +``` +states.each do |abbrev, full| + puts "#{full}" +end +``` 1. What is another example of when we might use a hash? In your example, why is a hash better than an array? + * Another example of when to use a hash would be if we were making a hash that contained names and residential addresses with names being the key and addresses being the value. In this case using a hash would be better than an array because the user would be able to look up the value knowing only what the key is. If we made an array the user would need to know what the index of the key and value is in order to look it up efficiently. 1. What questions do you still have about hashes? + * Hmm, I feel like I have so many still. Mainly about the details of how to call upon keys and values in a hash. Like in the exercise that was assigned in the exercise file I was unable to add a key and it's value to my hash with a `:` printing at the front of it. It's those details that I know I'll need to just work out as I go but they confuse me a little still. As well as calling upon methods with my hashes (and arrays for that matter), I have a very base knowledge of how to do this but it still feels a little shaky. Thankfully I have a good grasp of `each` thanks to the last few exercises being focused on that one! diff --git a/day_6/exercises/burrito.rb b/day_6/exercises/burrito.rb index 967f68b6c..6459ad305 100644 --- a/day_6/exercises/burrito.rb +++ b/day_6/exercises/burrito.rb @@ -1,19 +1,45 @@ -# Add the following methods to this burrito class and +# Add the following methods to this burrito class and # call the methods below the class: # 1. add_topping # 2. remove_topping # 3. change_protein class Burrito - attr_reader :protein, :base, :toppings + attr_accessor :protein, :base, :toppings + def initialize(protein, base, toppings) @protein = protein @base = base @toppings = toppings end + + def add_topping(extra) + @toppings.push(extra) + puts "I'll add #{extra}, please." + end + + def change_protein(change) + @protein = (change) + puts "I'll have #{protein} instead." + end + + def remove_topping(out) + @toppings.delete(out) + puts "Never mind, I don't want #{out} anymore." + end end dinner = Burrito.new("Beans", "Rice", ["cheese", "salsa", "guacamole"]) p dinner.protein p dinner.base p dinner.toppings +p dinner.add_topping "olives" +p dinner.toppings +p dinner.change_protein("Chicken") +p dinner.protein +p dinner.change_protein("Beef") +p dinner.remove_topping("cheese") +p dinner.toppings + +# Okay, so the add_topping and remove_topping really threw me off. +# I was able to work with a couple peers to help find the answers. diff --git a/day_6/exercises/classes_and_objects.rb b/day_6/exercises/classes_and_objects.rb new file mode 100644 index 000000000..547473d2c --- /dev/null +++ b/day_6/exercises/classes_and_objects.rb @@ -0,0 +1,109 @@ +# I rewrote this code a few times to grasp the understanding. +# I was able to jump on a study session with a couple peers and work my way +# through it. I still have a VERY basic understanding of what I'm doing here. + +# class GoodDog +# def initialize +# puts "This object was initialized!" +# end +# end +# +# sparky = GoodDog.new + +class GoodDog + attr_accessor :name, :height, :weight + + def initialize(n, h, w) + @name = n + @height = h + @weight = w + end + + def speak + "#{name} says arf!" + end + + def change_info(n, h, w) + self.name = n + self.height = h + self.weight = w + end + + def some_method + self.info + end + + def info + "#{self.name} weighs #{self.weight} and is #{self.height} tall." + end +end + +sparky = GoodDog.new("Sparky", "12 inches", "10lbs") +# puts sparky.speak +# puts sparky.name +# sparky.name = "Spartacus" +# puts sparky.name +puts sparky.info + +sparky.change_info("Spartacus", "24 inches", "45 lbs") +puts sparky.info + +# fido = GoodDog.new("Fido") +# puts fido.speak +puts sparky.some_method + + +class MyCar + attr_accessor :color + attr_reader :year + + def initialize(y, c, m) + @year = y + @color = c + @model = m + @current_speed = 0 + end + + def speed_up(speed) + @current_speed += speed + puts "When you press the gas your speed changes to #{speed} mph." + end + + def brake(speed) + @current_speed -= speed + puts "When you press the break your speed changes to #{speed} mph." + end + + def current_speed + puts "You're current speed is #{@current_speed} mph." + end + + def shut_car_off + @current_speed = 0 + end + + def spray_paint(change) + self.color = (change) + puts "Now, your car is #{color}" + end + + # I originally created this one and then added the attr_reader. I wanted to + # see how to get both to work. + # def set_year + # puts "#{@year}" + # end +end + +jeep = MyCar.new(2015, "red", "wrangler") +jeep.speed_up(50) +jeep.brake(30) +jeep.current_speed +jeep.shut_car_off +jeep.current_speed +puts '-' * 10 +puts jeep.color +jeep.color = "black" +puts jeep.color +puts jeep.year +# jeep.spray_paint = "green" +jeep.spray_paint("green") diff --git a/day_6/exercises/dog.rb b/day_6/exercises/dog.rb index 03221314d..6d63cb906 100644 --- a/day_6/exercises/dog.rb +++ b/day_6/exercises/dog.rb @@ -1,5 +1,5 @@ # In the dog class below, write a `play` method that makes -# the dog hungry. Call that method below the class, and +# the dog hungry. Call that method below the class, and # print the dog's hunger status. class Dog @@ -19,6 +19,10 @@ def bark def eat @hungry = false end + + def play + @hungry = true + end end fido = Dog.new("Bernese", "Fido", 4) @@ -28,3 +32,9 @@ def eat p fido.hungry fido.eat p fido.hungry +fido.play +p fido.hungry +fido.eat +p fido.hungry + +# I printed this a few times to ensure that I was able to get it to print out corretly. diff --git a/day_6/exercises/person.rb b/day_6/exercises/person.rb index 2c26e9570..a8260ff2d 100644 --- a/day_6/exercises/person.rb +++ b/day_6/exercises/person.rb @@ -1,5 +1,48 @@ -# Create a person class with at least 2 attributes and 2 behaviors. +# Create a person class with at least 2 attributes and 2 behaviors. # Call all person methods below the class and print results # to the terminal that show the methods in action. -# YOUR CODE HERE +class Person + attr_accessor :hair, :eye + + def initialize(hair, eye) + @hair = hair + @eye = eye + puts "I, a person, is born!" + end + + def color + puts "I have #{hair} hair." + end + + def dye_hair(hair) + @hair = "#{hair}" + puts "I actually love my #{hair} hair now!" + end + + def socket + puts "My eyes are #{eye}." + end + + def put_in_contacts(eye) + @eye = "#{eye}" + puts "After putting in my contacts my eyes are now #{eye}. Love that!" + end +end + +sally = Person.new("blue", "hazel") +sally.color +sally.dye_hair("green") +sally.color +sally.socket +sally.put_in_contacts("brown") +sally.socket + +puts '-' * 10 +fred = Person.new("black", "green") +fred.color +fred.dye_hair("peachy blonde") +fred.color +fred.socket +fred.put_in_contacts("blue") +fred.socket diff --git a/day_6/exercises/student.rb b/day_6/exercises/student.rb new file mode 100644 index 000000000..299690bd5 --- /dev/null +++ b/day_6/exercises/student.rb @@ -0,0 +1,53 @@ +class Student + attr_accessor :first_name, :last_name, :primary_phone_number + + def introduction(target) + puts "Hi #{target}, I'm #{first_name}!" + end + + def favorite_number + 7 + end +end + +frank = Student.new +frank.first_name = "Frank" +# frank.introduction('Katrina') +puts "Frank's favorite number is #{frank.favorite_}" + + +# This was a page I used to test my Dog class for my questions.md example. +class Dog + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end + + def nick_name(name) + @name = name + puts "Here, #{name}!" + end + + def birthday + @age += 1 + puts "Happy Birthday #{name}! How does #{age} years old feel?!" + end +end + +aussie = Dog.new("Hawk", 5) +puts aussie.name +puts aussie.age +print aussie.nick_name("Hawktapus") +puts aussie.name +print aussie.birthday +puts aussie.age + +retriever = Dog.new("Ted", 1) +puts retriever.name +puts retriever.age +print retriever.nick_name("Teddielicious") +puts retriever.name +print retriever.birthday +puts retriever.age diff --git a/day_6/exercises/what_are_objects.rb b/day_6/exercises/what_are_objects.rb new file mode 100644 index 000000000..c6dc8e5a2 --- /dev/null +++ b/day_6/exercises/what_are_objects.rb @@ -0,0 +1,82 @@ +class GoodDog +end + +sparky = GoodDog.new +fido = GoodDog.new +spot = GoodDog.new + +module Speak + def speak(sound) + puts sound + end +end + +class GoodDog + include Speak +end + +class HumanBeing + include Speak +end + +sparky = GoodDog.new +sparky.speak("Arf!") +bob = HumanBeing.new +bob.speak("Hello") + + +puts '-' * 10 + +module Speak + def speak(sound) + puts "#{sound}" + end +end + +class GoodDog + include Speak +end + +class HumanBeing + include Speak +end + +puts "---GoodDog ancestors---" +puts GoodDog.ancestors +puts '' +puts "---HumanBeing ancestors---" +puts HumanBeing.ancestors + +# Exercises + +# 1. # creating an object in Ruby +class ChangeJar +end + +penny = ChangeJar.new + +# 2. +# What is a module? What is its purpose? +# "A module is a collection of behaviors that is usable in +# other classes via mixins." +# I spent a bit of time looking up different resources to figure out what +# this actually meant and how I would explain it in my own words. After +# looking up a lot of articles and a few videos I want to explain it as a +# container for methods, classes and constants. Objects can't be created from +# modules. It purpose is to organize the methods, classes and constants that +# are being created. I'm still trying to grasp what a module is really for +# but I have a base idea at the moment. +# This was one video I looked at, it was helpful: +# https://www.youtube.com/watch?v=Cq_dKYAqMrI + +module Count +end + +class ChangeJar + include Count +end + +penny = ChangeJar.new + +# I looked at the solution to write this code. I will be diving deep into +# understanding modules. diff --git a/day_6/questions.md b/day_6/questions.md index f58ca5f71..e45b4a6ba 100644 --- a/day_6/questions.md +++ b/day_6/questions.md @@ -1,13 +1,73 @@ ## Day 6 Questions 1. In your own words, what is a Class? + * A `Class` is a blueprint for the objects in created in that `Class`. If we're talking about a theater play the `Class` is like the title of the play and the `objects` are like the scenes in the play. (This is a very loose idea of what a `Class` is) When a `Class` is created the creator will use CamelCase. A module is something different from a class; though they're similar, a module can not have objects created from it which is one of the biggest differences. + ``` + Example: + class = PostalMail + ``` 1. What is an attribute of a Class? + * An `attribute` of a class is something that every object or instance of that class will have. If we go back to our play example every play object or instance will `attributes` such as `length_of_time`, `amount_of_actors`, and `choreography`. + ``` + Example: + class = PostalMail + address, + postage_amount, + package_vessel + (every PostalMail class will have these attributes) + ``` 1. What is behavior of a Class? + * A `behavior` of a class is a method that changes an attribute. To use our play example we can have behaviors such as `audience_interruption`, `actor_breaks_leg`, `improv_dance_moves` that would change the `length_of_time`, `amount_of_actors`, and `choreography` attributes. These are actions that change an attribute of an object in the class. 1. In the space below, create a Dog class with at least 2 attributes and 2 behaviors: +``` +Example: +class Dog + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end + + def nick_name(name) + @name = name + puts "Here, #{name}!" + end + + def birthday + @age += 1 + puts "Happy Birthday #{name}! How does #{age} years old feel?!" + end +end + ``` 1. How do you create an instance of a class? +``` +Example: +class Dog + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end + + def nick_name(name) + @name = name + puts "Here, #{name}!" + end + + def birthday + @age += 1 + puts "Happy Birthday #{name}! How does #{age} years old feel?!" + end +end +aussie = Dog.new("Hawk", 5) +retriever = Dog.new("Ted", 1) +``` 1. What questions do you still have about classes in Ruby? + * Again, like the last few lessons I feel like I have a base understanding of what classes are, but still need to explore the intricacies of methods. I guess that's not really classes..I understand classes and the difference between a module and a class. Not enough to teach a class but enough to be a student in the classroom. diff --git a/day_7/10_speckled_frogs.rb b/day_7/10_speckled_frogs.rb new file mode 100644 index 000000000..584b3377e --- /dev/null +++ b/day_7/10_speckled_frogs.rb @@ -0,0 +1,20 @@ +def frogs(y) + while y > 1 + puts "#{y} speckled frogs sat on a log" + puts "eating some most delicious bugs." + puts "One jumped in the pool where its nice and cool," + puts "then there were #{y - 1} speckled frogs." + y -= 1 + end + puts "#{y} speckled frogs sat on a log" + puts "eating some most delicious bugs." + puts "One jumped in the pool where its nice and cool," + puts "then there were no more speckled frog!" +end + +frogs(5) + +# When doing this exercise I was able to get extention +# 2 to work but was not able to figure out extension 1. +# I was able to do one or the other, not both. +# I will be excited to find out how to do this. diff --git a/day_7/ceasar_cipher.md b/day_7/caesar_cipher.md similarity index 78% rename from day_7/ceasar_cipher.md rename to day_7/caesar_cipher.md index 7390a70bc..223de2f47 100644 --- a/day_7/ceasar_cipher.md +++ b/day_7/caesar_cipher.md @@ -1,6 +1,6 @@ -## Ceasar Cipher +## Caesar Cipher -Also known as a shift cipher, the Ceasar Cipher is one of the oldest and simplest encoding techniques. A Ceasar Cipher works by shifting the alphabet by a defined number of letters down the alphabet. For example, with a left shift of 3, 'D' would be replaced by 'A', 'E' would be replaced by 'B', and so on. See below for a full alphabet example with a left shift of 3: +Also known as a shift cipher, the Caesar Cipher is one of the oldest and simplest encoding techniques. A Caesar Cipher works by shifting the alphabet by a defined number of letters down the alphabet. For example, with a left shift of 3, 'D' would be replaced by 'A', 'E' would be replaced by 'B', and so on. See below for a full alphabet example with a left shift of 3: ``` plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ @@ -10,7 +10,7 @@ cipher: XYZABCDEFGHIJKLMNOPQRSTUVW Create a file named caesar_cipher.rb and within that file, write a program that will take any string, and encode it based on a shift value provided by the user. The interaction pattern for this program might look something like this: ``` -cipher = CeasarCipher.new +cipher = CaesarCipher.new cipher.encode("Hello World", 5) => "CZGGJ RJMGY" ``` diff --git a/day_7/caesar_cipher.rb b/day_7/caesar_cipher.rb new file mode 100644 index 000000000..6fc7a9bac --- /dev/null +++ b/day_7/caesar_cipher.rb @@ -0,0 +1,19 @@ +# https://medium.com/@alexander.virga/ruby-simple-string-encryption-shift-caesar-cipher-encoder-rot-9dedf06374d1 +# I did not create this, I copied it from the website listed above so that I could understand what each line does. +def encrypt(string, key) + string_to_ascii_array = string.chars#.map {|char| char.ord} + # shifted = string_to_ascii_array.map {|char| char+key} + # shifted.map { |char| char.chr }.join +end + + puts encrypt("Hello World", 5) + +# I utilized this a lot when writing my high_level.md file. What I +# did was comment out each line to comprehend what the line did +# in relation to the code. I then ran this code in my terminal +# to see the result. This was incredibly helpful in gaining a +# very basic comprehension how this code was created. I have an +# understanding that there are multiple ways to create a cipher. + +# This was another website that I referenced for understanding. +# https://www.rubyguides.com/2015/03/caesar-cipher-in-ruby/ diff --git a/day_7/fizzbuzz.rb b/day_7/fizzbuzz.rb new file mode 100644 index 000000000..148fd86fa --- /dev/null +++ b/day_7/fizzbuzz.rb @@ -0,0 +1,26 @@ +def numbers(low, high) + while low < high + if low % 3 == 0 && low % 5 == 0 + print "FizzBuzz, " + elsif low % 3 == 0 + print "Fizz, " + elsif low % 5 == 0 + print "Buzz, " + else + print "#{low}, " + end + low += 1 + end + if high % 3 == 0 && high % 5 == 0 + print "FizzBuzz." + elsif high % 3 == 0 + print "Fizz." + elsif high % 5 == 0 + print "Buzz." + else + print "#{high}." + end +end + +numbers(1, 100) +# numbers(33, 104) diff --git a/day_7/high_level.md b/day_7/high_level.md new file mode 100644 index 000000000..293428499 --- /dev/null +++ b/day_7/high_level.md @@ -0,0 +1,83 @@ +## Caesar Cipher + +### My thoughts on the one of the oldest methods of data encryption +A Caesar Cipher is going to take our input and create a coded message. In order to do this I looked up a few different methods. Below is a method I found from this website. `https://medium.com/@alexander.virga/ruby-simple-string-encryption-shift-caesar-cipher-encoder-rot-9dedf06374d1` I have a fairly basic understanding of this so I thought I would copy the method I copied from the website and explain what each line is doing. What I have found is that being able to type out the code and explain it that way. + +Below I have started to define the method, when this is put into a terminal we get an empty line. +``` +def encrypt(string, key) +end + +puts encrypt("Hello World", 5) +``` +Below I have added in `ascii` this is a set of 128 characters to complicate our code more than just using the alphabet. Adding in `chars` will turn our string into an array. +``` +def encrypt(string, key) + string_to_ascii_array = string.chars +end + +puts encrypt("Hello World", 5) +H +e +l +l +o + +W +o +r +l +d +``` +Below I have added in `.map {|char| char.ord}` this will encrypt my original string that has been converted into an array. +``` +def encrypt(string, key) + string_to_ascii_array = string.chars.map {|char| char.ord} +end + +puts encrypt("Hello World", 5) +72 +101 +108 +108 +111 +32 +87 +111 +114 +108 +100 +``` +Below I have shifted the encryption by `5` which is defined by my key. +``` +def encrypt(string) + string_to_ascii_array = string.chars.map {|char| char.ord} + shifted = string_to_ascii_array.map {|char| char+key} +end + +puts encrypt("Hello World", 5) +77 +106 +113 +113 +116 +37 +92 +116 +119 +113 +105 +``` +Below by adding in `shifted.map { |char| char.chr }` I have changed the code from integers to the ascii characters as we called above. By adding in `.join` at the end my code prints on one line versus one on top of another. +``` +def encrypt(string, key) + string_to_ascii_array = string.chars.map {|char| char.ord} + shifted = string_to_ascii_array.map {|char| char+key} + shifted.map { |char| char.chr }.join +end + +puts encrypt("Hello World", 5) +Mjqqt%\twqi +``` + +Going through and explaining it in this manner allowed me to understand step by step how to get the original string `"Hello World"` to `Mjqqt%\twqi `. This is my high level understanding of the Caesar Cipher.