From 83a2663f7485aea7a4ffeafb62aa12660b7ae3e4 Mon Sep 17 00:00:00 2001 From: KKH Date: Sun, 6 Dec 2020 02:29:26 -0500 Subject: [PATCH 01/14] Add day 1 --- day_1/ex1.rb | 11 ++++++++ day_1/ex11.rb | 12 ++++++++ day_1/ex2.rb | 7 +++++ day_1/ex3.rb | 35 +++++++++++++++++++++++ day_1/ex3_study_drill.rb | 5 ++++ day_1/ex4.rb | 30 ++++++++++++++++++++ day_1/ex5.rb | 26 +++++++++++++++++ day_1/ex6.rb | 48 ++++++++++++++++++++++++++++++++ day_1/exercises/interpolation.rb | 4 +-- day_1/exercises/loops.rb | 8 ++++-- day_1/exercises/numbers.rb | 6 ++-- day_1/exercises/strings.rb | 4 +-- day_1/exercises/variables.rb | 13 +++++---- day_1/questions.md | 23 +++++++++++++++ 14 files changed, 217 insertions(+), 15 deletions(-) create mode 100644 day_1/ex1.rb create mode 100644 day_1/ex11.rb create mode 100644 day_1/ex2.rb create mode 100644 day_1/ex3.rb create mode 100644 day_1/ex3_study_drill.rb create mode 100644 day_1/ex4.rb create mode 100644 day_1/ex5.rb create mode 100644 day_1/ex6.rb diff --git a/day_1/ex1.rb b/day_1/ex1.rb new file mode 100644 index 000000000..5761f6e69 --- /dev/null +++ b/day_1/ex1.rb @@ -0,0 +1,11 @@ +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.' + +# Study Drills +# puts "Another line." +puts "Only this line prints" diff --git a/day_1/ex11.rb b/day_1/ex11.rb new file mode 100644 index 000000000..e64aea659 --- /dev/null +++ b/day_1/ex11.rb @@ -0,0 +1,12 @@ +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 "Tell me a random number: " +randomNumber = gets.chomp +puts "Thanks! Your random number was #{randomNumber}" diff --git a/day_1/ex2.rb b/day_1/ex2.rb new file mode 100644 index 000000000..6e7e2ad0e --- /dev/null +++ b/day_1/ex2.rb @@ -0,0 +1,7 @@ +# This is a commented line + +puts "This is not a commented line" # but I can comment after my Code + +# puts "This will not show because I am putting my octothorp in front of my code" + +puts "This will definitely run, assuming I've formatted correctly!" diff --git a/day_1/ex3.rb b/day_1/ex3.rb new file mode 100644 index 000000000..d38dd7290 --- /dev/null +++ b/day_1/ex3.rb @@ -0,0 +1,35 @@ +# This should print a string +puts "I will now count my chickens:" +# Lines 9 & 10 should print an interpolated string with a numerical result +# the + operator should return the sum of numbers +# the - operator should return the difference of numbers +# the * operator should return the product of numbers +# the / operator should return the quotient of numbers +# the % operator should return the remainder of numbers +puts "Hens #{25.0 + 30.0 / 6.0}" +puts "Roosters #{100.0 - 25.0 * 3.0 % 4.0}" + +# This should return a string +puts "Now I will count the eggs:" +# This should return a numberical value +puts 3.0 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 + +# This should return a string +puts "Is it true that 3 + 2 < 5 - 7?" + +# This should return a boolean value of either true or false +puts 3.0 + 2 < 5 - 7 + +# Lines 24 & 25 should return a string interpolated with a numerical result +puts "What is 3 + 2? #{3.0 + 2}" +puts "What is 5 - 7? #{5.0 - 7}" + +# This should return a string +puts "Oh, that's why it's false." +# More strings +puts "How about some more." + +# LInes 33-36 should return an interpolated string with boolean results of true or false +puts "Is it greater? #{5.0 > -2}" +puts "Is it greater or equal? #{5.0 >= -2}" +puts "Is it less or equal? #{5.0 <= -2}" diff --git a/day_1/ex3_study_drill.rb b/day_1/ex3_study_drill.rb new file mode 100644 index 000000000..d1a07bf29 --- /dev/null +++ b/day_1/ex3_study_drill.rb @@ -0,0 +1,5 @@ +# This should calculate the square footage of my room +# My room is 14ft by 16ft. +# Area is calculated Length * Width + +puts "Your room's total area is #{14 * 16} ft. squared. Have a nice day!" diff --git a/day_1/ex4.rb b/day_1/ex4.rb new file mode 100644 index 000000000..179ea77f0 --- /dev/null +++ b/day_1/ex4.rb @@ -0,0 +1,30 @@ +# defined variable cars and assigned it to numerical value of 100 +cars = 100 +# defined variable space_in_a_car and assigned it to number value of 4 +space_in_a_car = 4 +# defined variable driver and assigned it to number value of 30 +drivers = 30 +# defined variable passengers and assigned it to number value of 90 +passengers = 90 +# defined variable cars_not_driven to be equal to the value of the difference between the variables cars and drivers +cars_not_driven = cars - drivers +# cars_driven variable is assigned to the numerical value assigned to drivers, which is 30 +cars_driven = drivers +# carpool_capacity variable is assigned to the numerical result of the product of the variables cars_driven and space_in_a_car +carpool_capacity = cars_driven * space_in_a_car +# average_passengers_per_car is assigned to the numerical value of the quotient of passengers and cars_driven +average_passengers_per_car = passengers / cars_driven + +puts "There are #{cars} cars available" +puts "There are only #{drivers} drivers available" +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 +# Error Message - Ruby shows an error at line 14 that the variable carpool_capacity was not appropriately defined +# 1. Your carpool_capacity number changes to an integer as a result of changing the number at space_in_a_car +# 4. In our case, the "=" operator is not necessarily the same value of the mathematical operator of "=". +# In our case, instead of being "equals", the "=" operator is instead to represent being "assigned". Hence, we use +# the "=" operator to assign variables to data types or to other variables or anything, really. diff --git a/day_1/ex5.rb b/day_1/ex5.rb new file mode 100644 index 000000000..821a3a50b --- /dev/null +++ b/day_1/ex5.rb @@ -0,0 +1,26 @@ +name = 'Zed A. Shaw' +age = 35 # not a lie in 2009 +height = 74 # inches +weight = 180 # lbs +eyes = 'Blue' +teeth = 'White' +hair = 'Brown' + +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}." + +# This should convert inches to centimeters +inches = 5 +pounds = 25 + +makeToCentimeter = "You have #{inches} inches and when it's converted to centimeters you get #{inches * 2.54}cm" +puts makeToCentimeter +makeToPounds = "You have #{pounds} pounds and when it's converted to kilograms you get #{pounds * 0.453592}kg" +puts makeToPounds diff --git a/day_1/ex6.rb b/day_1/ex6.rb new file mode 100644 index 000000000..e39413d90 --- /dev/null +++ b/day_1/ex6.rb @@ -0,0 +1,48 @@ +# variable types_of_people is assigned to the value of 10 +types_of_people = 10 +# variable x is assigned to an interpolated string +x = "There are #{types_of_people} types of people." +# variable binary is assigned to a string +binary = "binary" +# variable do_not is assigned to a string +do_not = "don't" +# y variable is assigned to an interpolated string +y = "Those who know #{binary} and those who #{do_not}." + +# prints to the console of the data assigned to the variable x +puts x +# prints to the console of the data assigned to the variable y +puts y + +# prints to the console an interpolated string +puts "I said: #{x}." +# prints to the console an interpolated string +puts "I also said: '#{y}'." + +# variable hilarious is assigned to the boolean data type of false +hilarious = false +# variable joke_evaluation is assigned to an interpolated string that +# also calls back another data type assigned to the hilarious variable +joke_evaluation = "Isn't that joke so funny?! #{hilarious}" + +# returns to the console the data assigned to the variable joke_evaluation +puts joke_evaluation + +# w variable is assigned to a string data type +w = "This is the left side of..." +# e variable is assigned to a string data type +e = "a string with a right side." + +# prints to the console the interpolated result of the variables w and e +puts w + e + +# Study Drills +# 3. There are 6 places that a string is inside a string. 5 if you don't include +# one line of code that has two instances of a string inside of a string. +# 4. Both w & e are variables with strings assigned to each variable. That's why +# calling a put to w & e will return in a longer string. +# 5. Sometimes putting a single quotation instead of a double quotation will cause +# problems because of the English syntax use of apostrophes, which utilize single +# quotes. The computer, because it reads both single and double quotes as strings, +# becomes confused when the human programmer "interpolates" English syntax useage +# of single and double quotes. diff --git a/day_1/exercises/interpolation.rb b/day_1/exercises/interpolation.rb index c7f4f47df..6cdaa660b 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 predictable result, the #{slow_poke} beat the #{speedy}" diff --git a/day_1/exercises/loops.rb b/day_1/exercises/loops.rb index 90dc15ab1..e97ad03c0 100644 --- a/day_1/exercises/loops.rb +++ b/day_1/exercises/loops.rb @@ -5,14 +5,18 @@ # Example: Write code that prints your name five times: 5.times do - p "Hermione Granger" + p "Kon Ham" 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..eacf3d3fb 100644 --- a/day_1/exercises/numbers.rb +++ b/day_1/exercises/numbers.rb @@ -7,10 +7,10 @@ p 2 + 2 # Write code that prints the result of 7 subtracted from 83: -p #YOUR CODE HERE +p 83 - 7 # Write code that prints the result of 6 multiplied by 53: -# YOUR CODE HERE +p 6 * 53 # Write code that prints the result of the modulo of 10 into 54: -# YOUR CODE HERE +p 54 % 10 diff --git a/day_1/exercises/strings.rb b/day_1/exercises/strings.rb index f2f903ffc..4e9dfe007 100644 --- a/day_1/exercises/strings.rb +++ b/day_1/exercises/strings.rb @@ -7,7 +7,7 @@ p "Alan Turing" # Write code that prints `Welcome to Turing!` to the terminal: -p #YOUR CODE HERE +p "Welcome to Turing!" # Write code that prints `99 bottles of pop on the wall...` to the terminal: -# YOUR CODE HERE +p "99 bottles of pop on the wall..." diff --git a/day_1/exercises/variables.rb b/day_1/exercises/variables.rb index a1e45bb26..a4192cbed 100644 --- a/day_1/exercises/variables.rb +++ b/day_1/exercises/variables.rb @@ -1,29 +1,30 @@ # 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 = "Kon Ham" 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 +p 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 +stringHarry = "Harry Potter must not return to Howarts!" +p stringHarry # Write code that adds 2 to the `students` variable and # prints the result: students = 22 # YOUR CODE HERE -p students +p students + 2 # Write code that subracts 2 from the `students` variable and # prints the result: # YOUR CODE HERE -p students +p students - 2 diff --git a/day_1/questions.md b/day_1/questions.md index 73700e323..3d35fe31c 100644 --- a/day_1/questions.md +++ b/day_1/questions.md @@ -2,16 +2,39 @@ 1. How would you print the string `"Hello World!"` to the terminal? +I would write `puts "Hello World!"` + 1. What character is used to indicate comments in a ruby file? +The `#`, or octothorp, is utilized to insert comments into a ruby file. + 1. Explain the difference between an integer and a float? +Integers have no floating decimal. That is, Any number without any decimal. Floats will have a decimal. + 1. In the space below, create a variable `animal` that holds the string `"zebra"` +`animal = "zebra"` + 1. How would you print the string `"zebra"` using the variable that you created above? +`puts animal` + 1. What is interpolation? Use interpolation to print a sentence using the variable `animal`. +Interpolation allows me to insert variables into a string. For example, + +``` +variable = "Hello" +puts "#{variable}, my name is Kon" +``` + 1. What method is used to get input from a user? +The `gets` method gets input from a user. + 1. Name and describe two common string methods: + +The first common string method would be the `.split` method. This allows the user to take a string that split that string into smaller pieces. + +Another common string method would be the `.length` method. This method will return a numerical value of the length of the string you are called in the method. From 692215b965f209325838682c06338026fc7a19dc Mon Sep 17 00:00:00 2001 From: KKH Date: Sun, 6 Dec 2020 02:30:28 -0500 Subject: [PATCH 02/14] Add day 1 --- day_1/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/day_1/README.md b/day_1/README.md index 034ae11da..d776697fc 100644 --- a/day_1/README.md +++ b/day_1/README.md @@ -80,7 +80,7 @@ Work through the files in the day_1/exercises directory. Complete them in this ## Questions - Each day contains a questions.md file where you will answer questions about what you have learned. -Answer the day 1 questions within the questions.md file. The `.md` file extension refers to markdown formatting. Markdown is a simple markup language to help format your text. [This article](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) shows everything you need for basic markdown formatting. +Answer the day 1 questions within the questions.md file. The `.md` file extension refers to markdown formatting. Markdown is a simple markup language to help for mat your text. [This article](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) shows everything you need for basic markdown formatting. ## Save your work in Git From db5723732e5933f540e5c7cfb2515bfb15c9a808 Mon Sep 17 00:00:00 2001 From: KKH Date: Sun, 6 Dec 2020 02:33:30 -0500 Subject: [PATCH 03/14] Add day 1 --- day_1/questions.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/day_1/questions.md b/day_1/questions.md index 3d35fe31c..8ea71325d 100644 --- a/day_1/questions.md +++ b/day_1/questions.md @@ -2,39 +2,39 @@ 1. How would you print the string `"Hello World!"` to the terminal? -I would write `puts "Hello World!"` + I would write `puts "Hello World!"` 1. What character is used to indicate comments in a ruby file? -The `#`, or octothorp, is utilized to insert comments into a ruby file. + The `#`, or octothorp, is utilized to insert comments into a ruby file. 1. Explain the difference between an integer and a float? -Integers have no floating decimal. That is, Any number without any decimal. Floats will have a decimal. + Integers have no floating decimal. That is, Any number without any decimal. Floats will have a decimal. 1. In the space below, create a variable `animal` that holds the string `"zebra"` -`animal = "zebra"` + `animal = "zebra"` 1. How would you print the string `"zebra"` using the variable that you created above? -`puts animal` + `puts animal` 1. What is interpolation? Use interpolation to print a sentence using the variable `animal`. -Interpolation allows me to insert variables into a string. For example, + Interpolation allows me to insert variables into a string. For example, -``` -variable = "Hello" -puts "#{variable}, my name is Kon" -``` + ``` + variable = "Hello" + puts "#{variable}, my name is Kon" + ``` 1. What method is used to get input from a user? -The `gets` method gets input from a user. + The `gets` method gets input from a user. 1. Name and describe two common string methods: -The first common string method would be the `.split` method. This allows the user to take a string that split that string into smaller pieces. + The first common string method would be the `.split` method. This allows the user to take a string that split that string into smaller pieces. -Another common string method would be the `.length` method. This method will return a numerical value of the length of the string you are called in the method. + Another common string method would be the `.length` method. This method will return a numerical value of the length of the string you are called in the method. From 2efa37980c361df64d0589d6178c4c6d56dddf14 Mon Sep 17 00:00:00 2001 From: KKH Date: Sun, 6 Dec 2020 02:36:14 -0500 Subject: [PATCH 04/14] Add day 1 --- day_1/README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/day_1/README.md b/day_1/README.md index d776697fc..5a120486d 100644 --- a/day_1/README.md +++ b/day_1/README.md @@ -48,21 +48,21 @@ This will open the day_1 directory in Atom. You should be able to see the direct 1. Check off the items below as you complete the steps you just read for each lesson. ***Remember to create a file containing your work for each lesson!*** - - [ ] [A Good First Program](https://learnrubythehardway.org/book/ex1.html) + - [x] [A Good First Program](https://learnrubythehardway.org/book/ex1.html) - - [ ] [Comments in Code](https://learnrubythehardway.org/book/ex2.html) + - [x] [Comments in Code](https://learnrubythehardway.org/book/ex2.html) - - [ ] [Numbers and Math](https://learnrubythehardway.org/book/ex3.html) + - [x] [Numbers and Math](https://learnrubythehardway.org/book/ex3.html) - - [ ] [Variables and Names](https://learnrubythehardway.org/book/ex4.html) + - [x] [Variables and Names](https://learnrubythehardway.org/book/ex4.html) - - [ ] [Strings](https://learnrubythehardway.org/book/ex5.html) + - [x] [Strings](https://learnrubythehardway.org/book/ex5.html) - - [ ] [More Strings](https://learnrubythehardway.org/book/ex6.html) + - [x] [More Strings](https://learnrubythehardway.org/book/ex6.html) - - [ ] [Asking for Input](https://learnrubythehardway.org/book/ex11.html) + - [x] [Asking for Input](https://learnrubythehardway.org/book/ex11.html) - - [ ] Have you created 7 `ex.rb` files with your code in them? + - [x] Have you created 7 `ex.rb` files with your code in them? 1. Work through the [Strings](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#3.-strings) and [Numbers](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#5.-numbers) sections from Ruby in 100 Minutes. For each of these sections, open an `irb` session by typing `irb` into your terminal and type in the code snippets provided. From c8e307f9df3d2c2d84d74d8c076525ca6de2052b Mon Sep 17 00:00:00 2001 From: KKH Date: Mon, 7 Dec 2020 15:59:57 -0500 Subject: [PATCH 05/14] Add day 1 --- day_1/exercises/variables.rb | 2 +- day_2/array_methods.md | 130 +++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 day_2/array_methods.md diff --git a/day_1/exercises/variables.rb b/day_1/exercises/variables.rb index a4192cbed..9d75af036 100644 --- a/day_1/exercises/variables.rb +++ b/day_1/exercises/variables.rb @@ -15,7 +15,7 @@ # Write code that saves the string 'Harry Potter must not return to Hogwarts!' # and prints what that variable holds to the terminal: -stringHarry = "Harry Potter must not return to Howarts!" +string_harry = "Harry Potter must not return to Howarts!" p stringHarry # Write code that adds 2 to the `students` variable and diff --git a/day_2/array_methods.md b/day_2/array_methods.md new file mode 100644 index 000000000..e6f704184 --- /dev/null +++ b/day_2/array_methods.md @@ -0,0 +1,130 @@ +# Array Methods Described + +### collect +The `.collect` method will return a new array that contains the elements modified with the value that I've specified. + +For example, +``` +$ array = ["bach", "mozart", "beethoven", "haydn"] +=> ["bach", "mozart", "beethoven", "haydn"] + +$ array.collect { |x| x + "@" } +=> ["bach@", "mozart@", "beethoven@", "haydn@"] +``` +### each +The `.each` method will allow me to iterate over each element in my array. + +For example, +``` +$ b = [5, 4, 3, 2, 1] +=> [5, 4, 3, 2, 1] + +$ b.each {|x| puts "My countdown is: #{x}"} +My countdown is: 5 +My countdown is: 4 +My countdown is: 3 +My countdown is: 2 +My countdown is: 1 +=> [5, 4, 3, 2, 1] +``` +### first and last +The `.first` method will allow me to return the first element of my array, or the first elements of my array. + +For example, +``` +$ c = ["a", "b", "c", "d", "e"] +=> ["a", "b", "c", "d", "e"] + +$ c.first +=> "a" + +$ c.first(3) +=> ["a", "b", "c"] +``` + +The `.last` method will allow me to return the last element of my array or the last 'nth' elements of my array. + +For example, +``` +$ d = [1, 2, 3, 4, 5, 6, 7] +=> [1, 2, 3, 4, 5, 6, 7] + +$ d.last +=> 7 + +$ d.last(3) +=> [5, 6, 7] +``` +### include? +The `.include` method will return a boolean value of true or false if the object exists in the given array. + +For example, +``` +e = [1, 2, 3, 4, 5, 6, 7] +=> [1, 2, 3, 4, 5, 6, 7] + +$ e.include?(5) +=> true + +$ e.include?(9) +=> false + +$ e.include?("big foot") +=> false +``` +### index +The `.index` method will return the index of a given element inside an array. You can use either a block or an argument format. + +For example, +``` +$ f = ["a", "b", "c", "d", "e", "f"] +=> ["a", "b", "c", "d", "e", "f"] + +$ f.index("e") +=> 4 + +$ f.index("z") +=> nil + +$ f.index {|x| x == "a"} +=> 0 +``` +### join +The `.join` method will, if an argument is not defined, return a string that joins all of the elements in the array into a string. If an argument is defined, the elements in the string will be joined with the specified argument. + +For example, +``` +$ a = ["a", "l", "i", "e", "n", "s"] +=> ["a", "l", "i", "e", "n", "s"] + +$ a.join +=> "aliens" + +$ a.join('-') +=> "a-l-i-e-n-s" +``` +### shuffle +The `.shuffle` method will return a shuffled array of my original array. + +For example, +``` +$ b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + +$ b.shuffle +=> [6, 10, 7, 9, 1, 5, 2, 4, 3, 8] +``` +### sort +The `.sort` method will take an unsorted array and sort according to numerical or alphabetical order. I can also go in reverse, if I apply the `.sort` method in a block form. + +For example, +``` +$ a = [1, 6, 3, 2, 5, 4] +=> [1, 6, 3, 2, 5, 4] + +$ a.sort +=> [1, 2, 3, 4, 5, 6] + +$ a.sort {|x, y| y <=> x} +=> [6, 5, 4, 3, 2, 1] +``` From aaa97d2caff452abb7347a478dfce72003a8b5ab Mon Sep 17 00:00:00 2001 From: KKH Date: Tue, 8 Dec 2020 02:42:08 -0500 Subject: [PATCH 06/14] Add Day 2 Work --- day_2/README.md | 6 +- day_2/exercises/arrays.rb | 16 +++--- day_2/exercises/ex28.rb | 80 ++++++++++++++++++++++++++ day_2/exercises/iteration.rb | 21 +++++-- day_2/exercises/iterations_and_each.rb | 63 ++++++++++++++++++++ day_2/questions.md | 15 +++-- 6 files changed, 179 insertions(+), 22 deletions(-) create mode 100644 day_2/exercises/ex28.rb create mode 100644 day_2/exercises/iterations_and_each.rb diff --git a/day_2/README.md b/day_2/README.md index 0c8c1571c..f8df9d04a 100644 --- a/day_2/README.md +++ b/day_2/README.md @@ -13,11 +13,11 @@ Using your terminal, open your local copy of the forked repository you created d 1. Work through the [Arrays](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#7.-arrays) section of Ruby in 100 Minutes. As you work through this section, research each of the methods mentioned by looking through the [Ruby docs for Arrays](https://ruby-doc.org/core-2.4.1/Array.html). Documentation like this might look intimidating, but diving in and practicing now will build your comfort level. Create a file in your day_2 directory called `array_methods.md` and describe what each method does in your own words. 1. Work through the following lessons. Any files that you create while working can be kept in today's `exercises` directory. - - [ ] Turing's [Iteration and Each](http://backend.turing.io/module1/lessons/iteration_and_each) lesson. + - [x] Turing's [Iteration and Each](http://backend.turing.io/module1/lessons/iteration_and_each) lesson. - - [ ] [Booleans](https://learnrubythehardway.org/book/ex27.html) from Learn Ruby the Hard Way. + - [x] [Booleans](https://learnrubythehardway.org/book/ex27.html) from Learn Ruby the Hard Way. - - [ ] [Boolean Practice](https://learnrubythehardway.org/book/ex28.html) from Learn Ruby the Hard Way. + - [x] [Boolean Practice](https://learnrubythehardway.org/book/ex28.html) from Learn Ruby the Hard Way. 1. Work through the exercise files in the day_2/exercises directory. Complete them in this order: 1. arrays diff --git a/day_2/exercises/arrays.rb b/day_2/exercises/arrays.rb index f572a5ae6..3f4bf9eaa 100644 --- a/day_2/exercises/arrays.rb +++ b/day_2/exercises/arrays.rb @@ -10,12 +10,13 @@ # Write code that stores an array of states in a variable, # then prints that array: -states = #YOUR CODE HERE +states = ["North Carolina", "Texas", "South Carolina"] p states # Write code that stores an array of foods in a variable, # then prints that array: -# YOUR CODE HERE +foods = ["rice", "sushi", "tuna", "ebi shrimp"] +p foods # Example: Write code that prints the number of elements # in your above array of animals: @@ -23,18 +24,19 @@ # 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[0] # Write code that prints the last item of your foods array: -# YOUR CODE HERE +p foods[-1] # Write code that adds "lion" to your animals array # and prints the result (Hint- use a method): -# YOUR CODE HERE +p animals.push('lion') # Write code that removes the last element from your foods array # and prints the result (Hint- use a method): # YOUR CODE HERE +p foods.pop() +p foods diff --git a/day_2/exercises/ex28.rb b/day_2/exercises/ex28.rb new file mode 100644 index 000000000..8ba233838 --- /dev/null +++ b/day_2/exercises/ex28.rb @@ -0,0 +1,80 @@ +# should read: true. actually reads: true. +true && true + +# should read: false. actually reads: false. +false && true + +# should read: false. actually reads: false. +1 == 1 && 2 == 1 + +# should read: true. actually reads: true. +"test" == "test" + +# should read: true. actually reads: true. +1 == 1 || 2 != 1 + +# should read: true. actually reads: true. +true && 1 == 1 + +# should read: false. actually reads: false. +false && 0 != 0 + +# should read: true. actually reads: true. +true || 1 == 1 + +# should read: false. actually reads: false. +"test" == "testing" + +# should read: false. actually reads: false. +1 != 0 && 2 == 1 + +# should read: true. actually reads: true. +"test" != "testing" + +# should read: false. actually reads: false. +"test" == 1 + +# should read: false. actually reads: true. +!(true && false) + +# should read: false. actually reads: false. +!(1 == 1 && 0 != 1) + +# should read: true. actually reads: false. +!(10 == 1 || 1000 == 1000) + +# should read: true. actually reads: false. +!(1 != 10 || 3 == 4) + +# should read: true. actually reads: true. +!("testing" == "testing" && "Zed" == "Cool Guy") + +# should read: false. actually reads: true. +1 == 1 && (!("testing" == 1 || 1 == 0)) + +# should read: false. actually reads: false. +"chunky" == "bacon" && (!(3 == 4 || 3 == 3)) + +# should read: false. actually reads: false. +3 == 3 && (!("testing" == "testing" || "Ruby" == "Fun")) + +# Study Drills + +# 1 - 2 +# == is equal. +# != is not equal. +# > greater than. +# < less than. +# >= greater than or equal to. +# <= less than or equal to. +# <=> combined comparison operator. +# === test equality. + +# 3 +4 === 3 # false + +4 <= 5 # true + +"apples" != "oranges" # true + +"bach" == "Bach" # false diff --git a/day_2/exercises/iteration.rb b/day_2/exercises/iteration.rb index a801cb4fc..fa85b7afe 100644 --- a/day_2/exercises/iteration.rb +++ b/day_2/exercises/iteration.rb @@ -15,14 +15,23 @@ # "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 -# 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 = ["mango", "borscht", "rye bread", "vodka"] +foods.each do |food| + p "Add #{food} 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 = [1, 2, 3, 4, 5, 6, 7] +numbers.each do |numero| + p numero + p numero +end diff --git a/day_2/exercises/iterations_and_each.rb b/day_2/exercises/iterations_and_each.rb new file mode 100644 index 000000000..699e72ebd --- /dev/null +++ b/day_2/exercises/iterations_and_each.rb @@ -0,0 +1,63 @@ +# Exercise 1: Doubles +array_of_numbers = [1, 2, 3, 4, 5] +array_of_numbers.each do |index| + puts index + puts index +end +# Triples +array_of_numbers.each do |index| + puts index + puts index + puts index +end + +# Exercise 2: Even #s only +array_of_numbers.each do |index| + puts index if index.even? +end + +# Odd numbers only +array_of_numbers.each do |index| + puts index if index.odd? +end + +# Exercise 3: +array_of_numbers.each do |index| + puts index * 2 +end + +# Exercise 4: Full Name +array_of_first_last_names = ['Alice Smith', 'Bob Evans', 'Roy Rogers'] +array_of_first_last_names.each do |full_name| + puts full_name +end + +# First Name +array_of_first_last_names.each do |first| + puts first.split.first +end + +array_of_first_last_names.each do |last| + puts last.split.last +end + +# Initials +array_of_first_last_names.each do |initials| + first = initials.split.first[0, 1] + last = initials.split.last[0, 1] + puts "#{first}. #{last}." +end + +# Last name & Length +array_of_first_last_names.each do |lastLength| + last = lastLength.split.last + length = lastLength.split.last.length + puts "Last Name: #{last} Length: #{length}" +end + +# Integer representing the length of all characters in array +sum = 0 +array_of_first_last_names.each do |splitted| + splitted = sum += splitted.gsub(" ", "").length + puts sum +end diff --git a/day_2/questions.md b/day_2/questions.md index a179f0b04..4fc61c063 100644 --- a/day_2/questions.md +++ b/day_2/questions.md @@ -1,17 +1,20 @@ ## 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"`? - +I would access the array by writing: `animals[1]` 1. How would you add `"lion"` to the `animals` array? - +I can use a push method to add 'lion' to my animals array. Like this: `animals.push('lion')` 1. Name and describe two additional array methods: +The `.shift` method, like the `.pop` method, will remove an element from an array. However, unlike `.pop`, `.shift` will remove an element from the **front** of the array. +Speaking of the front of the array, the `.unshift` method will work in a similar method to `.push`, except that the `.unshift` method will add an element from the **front** of the array. 1. What are the boolean values in Ruby? - +Boolean values result in either a true or false. I can use many different comparison operators to return a true or false result: `<`, `>`, `==`, `===`, and the list goes on! 1. In Ruby, how would you evaluate if `2` is equal to `25`? What is the result of this evaluation? - +`2 == 25`, and the result will be false. 1. In Ruby, how would you evaluate if `25` is greater than `2`? What is the result of this evaluation? +`2 < 25`, and the answer will be true From 9ef0a920511247ed2f231471d1247732da8a1771 Mon Sep 17 00:00:00 2001 From: ignored-comment Date: Wed, 9 Dec 2020 04:42:18 -0500 Subject: [PATCH 07/14] Add Day 3 Work --- day_3/README.md | 8 ++-- day_3/exercises/conditionals.rb | 16 +++++++ day_3/exercises/ex29.rb | 61 ++++++++++++++++++++++++ day_3/exercises/ex30.rb | 57 +++++++++++++++++++++++ day_3/exercises/ex31.rb | 80 ++++++++++++++++++++++++++++++++ day_3/exercises/if_statements.rb | 44 ++++++++++++++---- day_3/questions.md | 62 +++++++++++++++++++++++-- 7 files changed, 311 insertions(+), 17 deletions(-) create mode 100644 day_3/exercises/conditionals.rb create mode 100644 day_3/exercises/ex29.rb create mode 100644 day_3/exercises/ex30.rb create mode 100644 day_3/exercises/ex31.rb diff --git a/day_3/README.md b/day_3/README.md index d4534e0e1..5ce4795a3 100644 --- a/day_3/README.md +++ b/day_3/README.md @@ -12,13 +12,13 @@ Using your terminal, open your local copy of the forked repository you created d 1. Work through the following lessons. Any files that you create while working can be kept in today's `exercises` directory. - - [ ] [What If?](https://learnrubythehardway.org/book/ex29.html) from Learn Ruby the Hard Way. + - [x] [What If?](https://learnrubythehardway.org/book/ex29.html) from Learn Ruby the Hard Way. - - [ ] [Else and If](https://learnrubythehardway.org/book/ex30.html) from Learn Ruby the Hard Way. + - [x] [Else and If](https://learnrubythehardway.org/book/ex30.html) from Learn Ruby the Hard Way. - - [ ] [Making Decisions](https://learnrubythehardway.org/book/ex31.html) from Learn Ruby the Hard Way. + - [x] [Making Decisions](https://learnrubythehardway.org/book/ex31.html) from Learn Ruby the Hard Way. - - [ ] [Conditionals](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#9.-conditionals) from Ruby in 100 Minutes. + - [x] [Conditionals](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#9.-conditionals) from Ruby in 100 Minutes. 1. Work through the exercise files in the day_3/exercises directory. diff --git a/day_3/exercises/conditionals.rb b/day_3/exercises/conditionals.rb new file mode 100644 index 000000000..3c2c4b1ec --- /dev/null +++ b/day_3/exercises/conditionals.rb @@ -0,0 +1,16 @@ +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 + +water_status(5) +water_status(7) +water_status(8) +water_status(9) diff --git a/day_3/exercises/ex29.rb b/day_3/exercises/ex29.rb new file mode 100644 index 000000000..f1408dc48 --- /dev/null +++ b/day_3/exercises/ex29.rb @@ -0,0 +1,61 @@ +people = 10 +cats = 4 +dogs = 5 + +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 + +# Study Drills +# 1 +# The if statement checks to see if the condition is +# either true or false, and if it is, runs the +# following line of code. + +# 2 +# It needs to be indented so that it is easier to read. +# Having the indentation below our if statement helps +# us to know that the following line after the if statement +# is related to our if statement, or is conditional of that +# if statement. + +# 3 +# We might get a syntax error. + +# 4 +if 3 > 1 || 1 > 4 + puts "AYYYYYYYY" +end + +if 3 = 3 && 1 < 3 + puts "YA!" +end diff --git a/day_3/exercises/ex30.rb b/day_3/exercises/ex30.rb new file mode 100644 index 000000000..f50bfabfe --- /dev/null +++ b/day_3/exercises/ex30.rb @@ -0,0 +1,57 @@ +people = 25 +cars = 10 +trucks = 5 + + +if cars > people + puts "We should take the cars." +elsif cars < people + puts "We should not take the cars." +else + puts "We can't decide." +end + +if trucks > cars + puts "That's too many trucks." +elsif trucks < cars + puts "Maybe we can take the trucks." +else + puts "We still can't decide." +end + +if people > trucks + puts "Alright, let's just take the trucks." +else + puts "Fine, let's just stay home then." +end + +# Study Drills +# 1 +# Elsif and else are a chain of conditional Statements +# that follow our original if statement. Elsif can be +# thought of as an "otherwise" statement in English +# syntax. The else is the final conditional that executes +# assuming neither the original if nor the elsif conditionals +# are met. + +# 2 +# If the value stored at cars is less than +# the value stored at people or if the value +# stored at trucks is greater than the value +# stored at cars, +if cars < people || trucks > cars + # then print to the console, + # "Hello my dear test" + puts "Hello my dear test." +# otherwise, if the value at people is greater +# than the value at cars AND ALSO the value +# at cars is less than the value at trucks, +elsif people > cars && cars < trucks + # then print to the console "Dear hello test my" + puts "Dear hello test my." +# else, if none of the above conditions are met +else + # print to the console "Nothing checked out" + puts "Nothing checked out." +# end of my group of if/elsif statements. +end diff --git a/day_3/exercises/ex31.rb b/day_3/exercises/ex31.rb new file mode 100644 index 000000000..a524df2e9 --- /dev/null +++ b/day_3/exercises/ex31.rb @@ -0,0 +1,80 @@ +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 is a giant bear here eating a cheese cake. What do you do?" + puts "1. Take the cake." + puts "2. Scream at the bear." + puts "3. Challenge the bear to a dance-off" + + print "> " + bear = $stdin.gets.chomp + + if bear == "1" + puts "The bear eats your face off. Good job!" + elsif bear == "2" + puts "The bear eats your legs off. Good job!" + elsif bear == "3" + puts "The bear accepts your challenge. You and the bear begin dancing frantically! But there is no judge nor jury! You and the bear are cursed to dance forever!" + 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 + +else + puts "You stumble around and fall on a knife and die. Good job!" +end + +puts "There is a room at the end of the hall. You cannot go backwards, only forwards. Type 1 to proceed closer to the door." +print "> " + +door_hall = $stdin.gets.chomp + +if door_hall == "1" + puts "You begin to approach the door. In some ways, you feel the door approaching you." + puts "You've arrived at the door. You suddenly realize there is some kind of breathing, beating, living creature behind the door." + puts "The beating - you're sure is a heartbeat - loud enough to pound into your ears. The breathing as real as the door." + puts "You are not sure whether friend or foe. Only that the door must be opened." + puts "1. You open the door." + puts "2. Delay." + end + print "> " + + door_choice = $stdin.gets.chomp + if door_choice == "1" + puts "Terrified, you open the door. A blinding light reveals itself - your eyes respond with severe pain." + puts "Shielding your eyes, you fall on your knees hoping for a swift death. The beating, breathing of the creature grows louder. It roars!" + puts "..." + puts "..." + puts "..." + puts "...then suddenly, quiet. Your eyes begin to recover. You hear the twinkling of a bird. The bubbling sound water running through a cool creek. Laughter." + puts "You look up to see a place of peace, purpose, and adventure. Your eyes still recovering, you see off in the distance the backdrop of a beautiful valley. Rolling green hills. Beyond the valley a mountain stands high, mighty, and strong." + puts "You wonder if such a strong, massive landmark can ever be destroyed by the corrupting power of the Earthly man." + puts "You take your first step into this place. As you do, you wonder about this place, feeling like somehow you've been here once before." + elsif door_choice == "2" + puts "Time moves differently here. Not a second passes making your decision when suddenly, you notice horrific changes to your body." + puts "You beautiful hair, full of color, in an instant turns white, becoming the appearance of a poorly made bird's nest." + puts "The skin around your finger nails has withdrawn, shriveled." + puts "You want to scream but you choke, cough, and hack - as if dying from thirst" + puts "Frantic, you try to grab at the closest object around you for support - yet your body has lost its range of motion." + puts "You've become a living mummy" + puts "You'll never know what was behind that door - but at least you didn't have to make a decision." + else + puts "You chose...poorly." + end diff --git a/day_3/exercises/if_statements.rb b/day_3/exercises/if_statements.rb index a80b96840..fcb4c04a1 100644 --- a/day_3/exercises/if_statements.rb +++ b/day_3/exercises/if_statements.rb @@ -3,7 +3,7 @@ # 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" @@ -27,6 +27,19 @@ # Experiment with manipulating the value held in variable 'weather' # to print something other than 'coat' + weather = 'icy' + + if weather == 'sunny' + p "sunscreen" + elsif weather == 'rainy' + p "umbrella" + elsif weather == 'snowy' + p "coat" + elsif weather == 'icy' + p "yak traks" + else + p "good to go!" + end ################## # Using the num_quarters variable defined below, determine @@ -35,21 +48,24 @@ # 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 = 1 -puts "I have enough money for a gumball" -puts "I don't have enough money for a gumball" + if num_quarters >= 2 + puts "I have enough money for a gumball" + else num_quarters < 2 + puts "I don't have enough money for a gumball" + end ##################### # 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 +77,17 @@ # Experiment with manipulating the value held within both variables # to make sure all above conditions output what you expect. -cups_of_flour = 1 -has_sauce = true +cups_of_flour = 8 +has_sauce = false + + if cups_of_flour == 1 && has_sauce == true + print "I cannot make pizza" + elsif cups_of_flour == 5 && has_sauce == false + print "I cannot make pizza" + elsif cups_of_flour == 2 && has_sauce == true + print "I can make pizza" + elsif cups_of_flour == 3 && has_sauce == true + print "I can make pizza" + else + print "...your cups of flour and sauce are out of wack, man..." + end diff --git a/day_3/questions.md b/day_3/questions.md index db6170fa7..b2e41771a 100644 --- a/day_3/questions.md +++ b/day_3/questions.md @@ -1,13 +1,65 @@ ## Day 3 Questions -1. What is a conditional statement? Give three examples. +1. What is a conditional statement? Give three examples. -1. Why might you want to use an if-statement? + A conditional statement is a way to considering options in order to perform an action. Sometimes, the things we want to do in life cannot be done as a matter of "see" and "do". -1. What is the Ruby syntax for an if statement? + Sometimes, we must wait for the right time or the right place. Or in our case, sometimes we must wait for the right condition. -1. How do you add multiple conditions to an if statement? +* First Example: + + If I am accepted into college, I go to college. Otherwise, I might go to trade school. + +* Second Example: + + If I am hungry and I have $15 dollars to spare, I buy a pizza. Otherwise, I'll stay at home and eat mac 'n cheese! + +* Second Example: + + If I feel tired and I've done my homework, I will go to sleep. Otherwise, I'll do some stretches and take another stab at my homework! + +1. Why might you want to use an if-statement? + + Not always do I want to do something right away. I might want to do something *when my condition is right*. + + Sometimes, when I am hungry and I want to make sushi, I cannot make sushi unless I have the right ingredients. If only life was such that we could have everything we wanted without conditions! + +1. What is the Ruby syntax for an if statement? + ``` + if condition_written + action_step + else + action_step + end + ``` + +1. How do you add multiple conditions to an if statement? + ``` + if condition_written + action_step + elsif condition_written + action_step + elsif another_condition + action_step + else + action_step + end + ``` 1. Provide an example of the Ruby syntax for an if/elsif/else statement: + ``` + hungry = true + money = 5 + + if hungry == true && money < 5 + puts "Sorry, it's rice and beans for tonight" + elsif hungry == true && money > 5 + puts "We have the money and the mood! But shouldn't we eat at home to save a few bucks?" + else + puts "Dude - the mood and money don't match, man!" + end + ``` + +1. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? -1. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? + We could call methods that check conditions to be true or false, which may help our code to be a little more readable to humans. From c2cea197f819f2d2f11d3a2284bf66695c55c668 Mon Sep 17 00:00:00 2001 From: ignored-comment13 Date: Wed, 9 Dec 2020 22:04:17 -0500 Subject: [PATCH 08/14] Add Day 4 Work --- day_4/README.md | 8 ++-- day_4/exercises/ex18.rb | 22 ++++++++++ day_4/exercises/ex19.rb | 75 ++++++++++++++++++++++++++++++++ day_4/exercises/ex21.rb | 44 +++++++++++++++++++ day_4/exercises/launch_method.rb | 21 +++++++++ day_4/exercises/methods.rb | 22 +++++++--- day_4/questions.md | 27 ++++++++++-- 7 files changed, 206 insertions(+), 13 deletions(-) create mode 100644 day_4/exercises/ex18.rb create mode 100644 day_4/exercises/ex19.rb create mode 100644 day_4/exercises/ex21.rb create mode 100644 day_4/exercises/launch_method.rb diff --git a/day_4/README.md b/day_4/README.md index 5fb1100bc..ded55347f 100644 --- a/day_4/README.md +++ b/day_4/README.md @@ -14,13 +14,13 @@ Using your terminal, open your local copy of the forked repository you created d _*Note*: In some of these lessons, the author refers to methods as functions. They are interchangable here, but at Turing, we will be use the word `method`._ - - [ ] [Methods](https://launchschool.com/books/ruby/read/methods) from LaunchSchool. Work up to the `obj.method or method(obj)` header. + - [x] [Methods](https://launchschool.com/books/ruby/read/methods) from LaunchSchool. Work up to the `obj.method or method(obj)` header. - - [ ] [Intro to Methods](https://learnrubythehardway.org/book/ex18.html) from Learn Ruby the Hard Way. + - [x] [Intro to Methods](https://learnrubythehardway.org/book/ex18.html) from Learn Ruby the Hard Way. - - [ ] [Methods and Variables](https://learnrubythehardway.org/book/ex19.html) from Learn Ruby the Hard Way. + - [x] [Methods and Variables](https://learnrubythehardway.org/book/ex19.html) from Learn Ruby the Hard Way. - - [ ] [Methods and Return Values](https://learnrubythehardway.org/book/ex21.html) from Learn Ruby the Hard Way. + - [x] [Methods and Return Values](https://learnrubythehardway.org/book/ex21.html) from Learn Ruby the Hard Way. 1. Work through the methods.rb file in the day_4/exercises directory. diff --git a/day_4/exercises/ex18.rb b/day_4/exercises/ex18.rb new file mode 100644 index 000000000..a58acf7ea --- /dev/null +++ b/day_4/exercises/ex18.rb @@ -0,0 +1,22 @@ +def print_two(*args) + arg1, arg2 = args + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +def print_two_again(arg1, arg2) + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +def print_one(arg1) + puts "arg1: #{arg1}" +end + +def print_none() + puts "I got nothin'." +end + + + print_two("Zed", "Shaw") + print_two_again("Zed", "Shaw") + print_one("First!") + print_none() diff --git a/day_4/exercises/ex19.rb b/day_4/exercises/ex19.rb new file mode 100644 index 000000000..765ab3f50 --- /dev/null +++ b/day_4/exercises/ex19.rb @@ -0,0 +1,75 @@ +# Define a method called cheese_and crackers +# set parameters of cheese_count and boxes_of_crackers +def cheese_and_crackers(cheese_count, boxes_of_crackers) + # returns a statement that interpolates a string and argument + # called to cheese_count parameter + puts "You have #{cheese_count} cheeses!" + # returns a statement that interpolates a string and argument + # called to the boxes_of_crackers parameter + puts "You have #{boxes_of_crackers} boxes of crackers!" + # returns a basic string + puts "Man that's enough for a party!" + # returns a basic string + puts "Get a blanket.\n" +# end statement indicates the end of the method +end + +# statement to indicate what we can do when calling our method. +puts "We can just give the function numbers directly:" +# here we are passing arguments of 20, and 30 into our parameters +# cheese_count and boxes_of_crackers, respectively. +cheese_and_crackers(20, 30) + +# string indicating an alternative we can do. this time +# we are passing numeric values assigned to variables +# as arguments in our methods. +puts "OR, we can use variables from our script:" +# assigning a value of 10 to the variable amount_of_cheese +amount_of_cheese = 10 +# assigning a value of 50 to the variable amount_of_crackers +amount_of_crackers = 50 + +# here we call the method cheese_and_crackers with an argument of +# amount_of_cheese and amount_of_crackers +cheese_and_crackers(amount_of_cheese, amount_of_crackers) + + +# returns string explaining the next step, which is to pass +# mathematical expressions inside of our method arguments +puts "We can even do math inside too:" +# method is called with mathematical expression of 10 + 20 +# called to the argument of our parameter at cheese_count +# and 5 + 6 mathematical expression passed to the second +# argument at the parameter boxes_of_crackers +cheese_and_crackers(10 + 20, 5 + 6) + +# returns a string explaining that we can use both variables +# and mathematical expressions to be passed as arguments to +# our method. +puts "And we can combine the two, variables and math:" +# cheese_and_crackers method called with amount_of_cheese variable +# and the mathematical expression of the numerical data assigned at +# that variable + 100. the second argument is passed as the variable +# amount_of_crackers and the mathematical expression of the numerical +# data assigned to that variable + 1000. +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) + +def one_sentence_one_word(words) + puts words +end + +one_sentence_one_word("This") +one_sentence_one_word("is") +one_sentence_one_word("the") +one_sentence_one_word("song") +one_sentence_one_word("that") +one_sentence_one_word("doesn't") +one_sentence_one_word("end...") +one_sentence_one_word("And") +one_sentence_one_word("it") +one_sentence_one_word("goes") +one_sentence_one_word("on") +one_sentence_one_word("and") +one_sentence_one_word("on") +one_sentence_one_word("my") +one_sentence_one_word("friend!") diff --git a/day_4/exercises/ex21.rb b/day_4/exercises/ex21.rb new file mode 100644 index 000000000..b9bb64685 --- /dev/null +++ b/day_4/exercises/ex21.rb @@ -0,0 +1,44 @@ +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 extra credit, type it in anyway. +puts "Here is a puzzle." + +what = multiply(age, subtract(height, divide(weight, divide(iq, 2)))) + +puts "That becomes #{what}. Can you do it by hand?" + + +# Study drills +# formula = (5 * 3 + (4 - 3) / (2 * 4) + +formula = divide(add(multiply(5, 3), subtract(4, 3)), multiply(2, 4)) +puts formula diff --git a/day_4/exercises/launch_method.rb b/day_4/exercises/launch_method.rb new file mode 100644 index 000000000..bc68bba6e --- /dev/null +++ b/day_4/exercises/launch_method.rb @@ -0,0 +1,21 @@ +puts "hello" +puts "hi" +puts "how are you" +puts "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 diff --git a/day_4/exercises/methods.rb b/day_4/exercises/methods.rb index 6ed338e5d..600d54823 100644 --- a/day_4/exercises/methods.rb +++ b/day_4/exercises/methods.rb @@ -12,16 +12,28 @@ def print_name # Write a method that takes a name as an argument and prints it: def print_name(name) - # YOUR CODE HERE + p name end 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 -# 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". +def adding(num1, num2) + p num1 + num2 +end + +adding(9000, 1) + +# 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 concat_string(string1, string2) + p "#{string1} #{string2}" +end + +concat_string("My Mellow,", "Yellow Cello") diff --git a/day_4/questions.md b/day_4/questions.md index af17ab4da..3f16e4697 100644 --- a/day_4/questions.md +++ b/day_4/questions.md @@ -1,11 +1,30 @@ ## Day 4 Questions -1. In your own words, what is the purpose of a method? +1. In your own words, what is the purpose of a method? -1. Create a method named `hello` that will print `"Sam I am"`. + Methods are ways to reuse multiple lines of code without having to rewrite them over and over again. Sometimes I will have a concept that involves multiple items of a same procedure. -1. Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`. + If that's the case, I can write a method once and reuse that method over and over again instead of having to rewrite the same lines of code over and over again. It's like recycling a water bottle instead of going out to the store to buy a new plastic water bottle every time I want to drink some water. -1. How would you call or execute the method that you created above? +1. Create a method named `hello` that will print `"Sam I am"`. + + ``` + def hello() + p "Sam I am" + end + ``` + +1. Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`. + ``` + def hello_someone(name) + p "#{name} I am" + end + ``` + +1. How would you call or execute the method that you created above? + + `hello_someone('Kon')` 1. What questions do you have about methods in Ruby? + + The effects of return, p, puts, and print and how they all work in conjunction or in which case I would use over the other. From 84ea1e7f5ff61e787dc53b7bde571f9e8cbcda2e Mon Sep 17 00:00:00 2001 From: ignored-comment Date: Fri, 11 Dec 2020 03:01:06 -0500 Subject: [PATCH 09/14] Add Day 5 Work --- day_5/README.md | 6 +- day_5/exercises/ex39.rb | 145 +++++++++++++++++++++++++++ day_5/exercises/hashes.rb | 29 ++++-- day_5/exercises/jump_start_hashes.rb | 13 +++ day_5/questions.md | 45 +++++++-- 5 files changed, 219 insertions(+), 19 deletions(-) create mode 100644 day_5/exercises/ex39.rb create mode 100644 day_5/exercises/jump_start_hashes.rb diff --git a/day_5/README.md b/day_5/README.md index 28be6b8fb..b9009751d 100644 --- a/day_5/README.md +++ b/day_5/README.md @@ -10,11 +10,11 @@ Using your terminal, open your local copy of the forked repository you created d ## Hash Lessons -1. Work through the following lessons. Any files that you create while working can be kept in today's `exercises` directory. +1. Work through the following lessons. Any files that you create while working can be kept in today's `exercises` directory. - - [ ] [Hashes](https://learnrubythehardway.org/book/ex39.html) from Learn Ruby the Hard Way. + - [x] [Hashes](https://learnrubythehardway.org/book/ex39.html) from Learn Ruby the Hard Way. - - [ ] [Hashes](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#8.-hashes) from Ruby in 100 minutes. + - [x] [Hashes](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#8.-hashes) from Ruby in 100 minutes. 1. Work through the hashes.rb file in the day_5/exercises directory. diff --git a/day_5/exercises/ex39.rb b/day_5/exercises/ex39.rb new file mode 100644 index 000000000..82be6865c --- /dev/null +++ b/day_5/exercises/ex39.rb @@ -0,0 +1,145 @@ +# create a mapping of state to abbreviation +states = { + 'Oregon' => 'OR', + 'Florida' => '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'] = 'Portland' + +# 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 "Florida's abbreviation is: #{states['Florida']}" + +# do it by using the state then cities dict +puts '-' * 10 +puts "Michigan has: #{cities[states['Michigan']]}" +puts "Florida has: #{cities[states['Florida']]}" + +# 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 + +# now 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 +# 1 +puts '-' * 20 +south_east_states = { + 'North Carolina' => 'NC', + 'South Carolina' => 'SC', + 'Tennessee' => 'TN', + 'Georgia' => 'GA', + 'Virginia' => 'VA' +} + +south_east_cities = { + 'NC' => 'Charlotte', + 'SC' => 'Colombia', + 'GA' => 'Atlanta' +} + +south_east_cities['TN'] = 'Nashville' +south_east_cities['VA'] = 'Richmond' + +puts '-' * 10 +puts "NC has #{south_east_cities['NC']}" +puts "SC has #{south_east_cities['SC']}" + +puts '-' * 10 +puts "Georgia's abbreviation is #{south_east_states['Georgia']}" +puts "Tennessee's abbreviation is #{south_east_states['Tennessee']}" + +puts '-' * 10 +puts "South Carolina has #{south_east_cities[south_east_states['South Carolina']]}" +puts "North Carolina has #{south_east_cities[south_east_states['North Carolina']]}" + +puts '-' * 10 +south_east_states.each do |state, abbrev| + puts "#{state} is abbreviated #{abbrev}" +end + +puts '-' * 10 +south_east_cities.each do |abbrev, city| + puts "#{abbrev} has city #{city}" +end + +puts '-' * 10 +south_east_states.each do |states, abbrev| + city = south_east_cities[abbrev] + puts "#{states} is abbreviated #{abbrev} and has the city #{city}" +end + +puts '-' * 10 +singular_state = south_east_states['Missouri'] + +if !singular_state + puts "Sorry, no Missouri" +end + +singular_city = south_east_cities['MO'] +singular_city ||= 'Does not Exist' +puts "The city for the state 'MO' is #{singular_city}" + +# 2 +puts '*' * 10 +south_east_states.default = "No such state exists in this hash - Sorry!" +puts "Searching for 'NY' in my south_east_states hash using the .default method..." +puts south_east_states['NY'] + +puts '*' * 10 +puts "Since you might be wondering what states do exist in my 'south_east_states' array, I've used the .each_key method to show you!" +puts south_east_states.each_key {|key| puts key} + +puts '*' * 10 +puts "What if I wanted to know if my south_east_states array was empty? Check 'dis out!" +puts south_east_states.empty? + +# 3 +# Hashes can't be ordered. We can however, use ennumerals to organize the values +# data by utilizing a <=> comparison operator. + +puts south_east_cities.sort_by {|abbrev, array| array} diff --git a/day_5/exercises/hashes.rb b/day_5/exercises/hashes.rb index 99fcebb77..f0d5e8e13 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,30 @@ p foods # Write code that prints a hash holding zoo animal inventory: -zoo = #YOUR CODE HERE +zoo = { + "monkey" => 5, + "zebra" => 3, + "lion" => 1, + "lionness" => 2, + "penguin" => 7, + "aligator" => 1 +} 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 +p zoo.keys -# 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 +p zoo.values -# 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 +p zoo['monkey'] -# 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['seal'] = 3 + +p zoo diff --git a/day_5/exercises/jump_start_hashes.rb b/day_5/exercises/jump_start_hashes.rb new file mode 100644 index 000000000..170e44b17 --- /dev/null +++ b/day_5/exercises/jump_start_hashes.rb @@ -0,0 +1,13 @@ +produce = {"apples" => 3, "oranges" => 1, "carrots" => 12} +puts "There are #{produce['oranges']} oranges in the fridge." + +produce["grapes"] = 221 +produce + +produce["oranges"] = 6 +produce +produce.keys +produce.values + +produce = {apples: 3, oranges: 1, carrots: 12} +puts "There are #{produce[:oranges]} oranges in the fridge." diff --git a/day_5/questions.md b/day_5/questions.md index d059e12c6..d17203c65 100644 --- a/day_5/questions.md +++ b/day_5/questions.md @@ -1,13 +1,46 @@ ## Day 5 Questions -1. What is a Hash, and how is it different from an Array? +1. What is a Hash, and how is it different from an Array? -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. + A `hash` is a collection of data that is organized by keys and values. It is not an ordered list. Keys are what I would call to access the values. If we use the concept of a dictionary, a key is the word in a dictionary and the value is the definition of the word in the dictionary. -1. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? + Arrays are lists that do not keys. It can be said that arrays have values but there is no key associated with each value. -1. With the same hash above, how would we get all the keys? How about all the 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. +``` +pet_store = { + "dog_collars" => 12, + "dog_food_jumbo_bag" => 4, + "dog_food_small_bag" => 13, + "cat_litter_jumbo_bag" = 1, + "cat_litter_small_bag" = 9 +} +``` +1. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? -1. What is another example of when we might use a hash? In your example, why is a hash better than an array? + I would access the value by typing `p states['IA']` -1. What questions do you still have about hashes? +1. With the same hash above, how would we get all the keys? How about all the values? + + keys: `p states.keys` + values: `p states.values` + +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 using a hash is when I need to make a list of items that require both a name and a value. Because arrays only hold a list of values, sometimes this is not sufficient enough for the type of data I want to organize. + + If I wanted to organize the variety of paper colors that I can buy from an office supply store, I can do so with an array. + + Consider, + + ``` + choice_of_paper_colors = ["ocean", "eggshell", "lavender", "gleam white"] + ``` + + But because I utilize one variable for a list of different values, practically speaking it is best to limit my array to a list that is relevant to my given category. + + Hashes give me a wider variety of expression when it comes to data organization. Because I have both `key` and `value`, I am no longer limited to one single category, but multiple categories. + +1. What questions do you still have about hashes? + + There is a lack of depth in my understanding of enumeration, hashes being unable to be sorted, and the consequences of those two concepts. From 3c9c94018d8c0830e5eda89dad55ad809252bc49 Mon Sep 17 00:00:00 2001 From: ignored-comment Date: Sat, 12 Dec 2020 00:44:40 -0500 Subject: [PATCH 10/14] Add Day 6 Work --- day_6/README.md | 8 +- day_6/exercises/burrito.rb | 33 ++- day_6/exercises/dog.rb | 8 +- .../jump_start_objects_attr_methods.rb | 17 ++ ...launch_school_classes_and_objects_part1.rb | 253 ++++++++++++++++++ .../launch_school_classes_define_objects.rb | 66 +++++ .../launch_school_what_are_objects.rb | 22 ++ day_6/exercises/person.rb | 51 +++- day_6/questions.md | 46 +++- 9 files changed, 492 insertions(+), 12 deletions(-) create mode 100644 day_6/exercises/jump_start_objects_attr_methods.rb create mode 100644 day_6/exercises/launch_school_classes_and_objects_part1.rb create mode 100644 day_6/exercises/launch_school_classes_define_objects.rb create mode 100644 day_6/exercises/launch_school_what_are_objects.rb diff --git a/day_6/README.md b/day_6/README.md index 829fe4809..fcc781101 100644 --- a/day_6/README.md +++ b/day_6/README.md @@ -11,13 +11,13 @@ Using your terminal, open your local copy of the forked repository you created d 1. Work through the following lessons. Any files that you create while working can be kept in today's `exercises` directory. - - [ ] [What Are Objects](https://launchschool.com/books/oo_ruby/read/the_object_model#whatareobjects) section from LaunchSchool. + - [x] [What Are Objects](https://launchschool.com/books/oo_ruby/read/the_object_model#whatareobjects) section from LaunchSchool. - - [ ] [Classes Define Objects](https://launchschool.com/books/oo_ruby/read/the_object_model#classesdefineobjects) section from LaunchSchool. + - [x] [Classes Define Objects](https://launchschool.com/books/oo_ruby/read/the_object_model#classesdefineobjects) section from LaunchSchool. - - [ ] [Classes and Objects Part 1](https://launchschool.com/books/oo_ruby/read/classes_and_objects_part1) from LaunchSchool. + - [x] [Classes and Objects Part 1](https://launchschool.com/books/oo_ruby/read/classes_and_objects_part1) from LaunchSchool. - - [ ] [Objects, Attributes and Methods](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#11.-objects,-attributes,-and-methods) from Ruby in 100 Minutes. + - [x] [Objects, Attributes and Methods](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#11.-objects,-attributes,-and-methods) from Ruby in 100 Minutes. 1. Work through the files in the day_6/exercises directory. diff --git a/day_6/exercises/burrito.rb b/day_6/exercises/burrito.rb index 967f68b6c..876089306 100644 --- a/day_6/exercises/burrito.rb +++ b/day_6/exercises/burrito.rb @@ -1,19 +1,48 @@ -# 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_reader :base, :toppings + attr_accessor :protein + def initialize(protein, base, toppings) @protein = protein @base = base @toppings = toppings end + + def add_topping(topping) + toppings.push(topping) + end + + def remove_topping(topping) + toppings.delete(topping) + end + + def change_protein(protein) + self.protein = protein + end + end dinner = Burrito.new("Beans", "Rice", ["cheese", "salsa", "guacamole"]) p dinner.protein p dinner.base p dinner.toppings + +p '-' * 25 +p "Adding the topping of 'sour cream'..." +p dinner.add_topping("sour cream") +p '-' * 25 + +p "Removing the topping 'guacamole'..." +p dinner.remove_topping("guacamole") +p "Checking my array toppings now..." +p dinner.toppings + +p '-' * 25 +p "Changing my protein to 'chicken...'" +p dinner.change_protein("Chicken") diff --git a/day_6/exercises/dog.rb b/day_6/exercises/dog.rb index 03221314d..0b3a9369a 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,5 @@ def eat p fido.hungry fido.eat p fido.hungry +fido.play +p fido.hungry diff --git a/day_6/exercises/jump_start_objects_attr_methods.rb b/day_6/exercises/jump_start_objects_attr_methods.rb new file mode 100644 index 000000000..9e23b41c5 --- /dev/null +++ b/day_6/exercises/jump_start_objects_attr_methods.rb @@ -0,0 +1,17 @@ +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_number}." diff --git a/day_6/exercises/launch_school_classes_and_objects_part1.rb b/day_6/exercises/launch_school_classes_and_objects_part1.rb new file mode 100644 index 000000000..d3abd588c --- /dev/null +++ b/day_6/exercises/launch_school_classes_and_objects_part1.rb @@ -0,0 +1,253 @@ +=begin +class GoodDog + # this initialize method is referred to as a constructor + # because it gets triggered whenever we create a new object + def initialize + puts "This object was initialized!" + end +end + +sparky = GoodDog.new + +# creating a new object and instantiating it with a state, like a name +class GoodDog + def initilize(name) + @name = name + end +end + +# @name is called an instance variable. it's a way to tie in data to objects +# instance variable will exist as long as object exists. see the above statement +# about being tied in + +# passing arguments into initalize method with the `new` method. + +sparky = GoodDog.new("Sparky") + +# on line 24 "Sparky" is being passed into the `new` method through the initalize method. +# the instance variable @name is tied with the string "Sparky" + +#instance methods + +class GoodDog + def intialize(name) + @name = name + end + + def speak + "Arf!" + end +end + +sparky = GoodDog.new("Sparky") +puts sparky.speak + +fido = GoodDog.new("Fido") +puts fido.speak + +# Changing the speak method to interpolate @name + says arf +def speak + "#{@name} says arf!" +end + +# accessor methods +puts sparky.name +# doesn't work + +# we have to create a method that will return the name +class GoodDog + def initialize(name) + @name = name + end + + def get_name + @name + end + + def speak + "#{@name} says arf!" + end +end + +sparky = GoodDog.new("Sparky") +puts sparky.speak +puts sparky.get_name + +# above we made a "getter" method with the sole purpose to only "get" Sparky's name. +# now we gon' make a "setter" method - this will change Sparky's name + +class GoodDog + def initialize(name) + @name = name + end + + def get_name + @name + end + + def set_name=(name) + @name = name + end + + def speak + "#{@name} says arf!" + end +end + +sparky = GoodDog.new("Sparky") +puts sparky.speak +puts sparky.get_name +sparky.set_name = "Spartacus" +puts sparky.get_name + +# snytactical sugar, apparently +# aligning naming conventions + +class GoodDog + def initialize(name) + @name = name + end + + def name + @name + end + + def name=(n) + @name = n + end + + def speak + "#{@name} says arf!" + end +end + +sparky = GoodDog.new("Sparky") +puts sparky.speak +puts sparky.name + +sparky.name = "Spartacus" +puts sparky.name + +class GoodDog + attr_accessor :name + + def initialize(name) + @name = name + end + + def speak + "#{@name} says arf!" + end +end + +sparky = GoodDog.new("Sparky") +puts sparky.speak +puts sparky.name +sparky.name = "Spartacus" +puts sparky.name + +# more accessor methods (it's getting cray cray up in here!) +# this time we take 6 instance methods and 3 instance variables into one method +# (name, name=, height, height=, weight, weight=) + +def change_info(n, h, w) + @name = n + @height = h + @weight = w +end + +#now our class GoodDog can be rewritten like dis': + +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) + @name = n + @height = h + @weight = w + end + + def info + "#{name} weights #{weight} and is #{height} tall." + end +end + +sparky = GoodDog.new('Sparky', '12 inches', '10 lbs') +sparky.change_info('Spartacus', '24 inches', '45 lbs') + +def change_info(n, h, w) + self.name = n + self.height = h + self.weight = w +end +=end + +# Exercises +# 1 + +class MyCar + attr_accessor :color + attr_reader :year + + def initialize(year, model, color) + @year = year + @model = model + @color = color + @current_speed = 0 + end + + def speed_up(number) + @current_speed += number + puts "You push the gas and accelerate #{number} mph." + end + + def brake(number) + @current_speed -= number + puts "You push the brake and decelerate #{number} mph" + end + + def current_speed + puts "You are now going #{@current_speed} mph." + end + + def shut_down + @current_speed = 0 + puts "Let's park this bad boy!" + end + + def spray_paint(color) + self.color = color + puts "Your new #{color} paint job looks great!" + end +end + +f150 = MyCar.new(2016, "Ford F-150", "black") +f150.speed_up(100) +f150.current_speed +f150.speed_up(50) +f150.current_speed +f150.brake(100) +f150.current_speed +f150.brake(10) +f150.current_speed +f150.shut_down +f150.current_speed + +# 2 +puts '-' * 20 +f150.color = "red" +puts f150.color +puts f150.year + +# 3 +f150.spray_paint('hot pink') diff --git a/day_6/exercises/launch_school_classes_define_objects.rb b/day_6/exercises/launch_school_classes_define_objects.rb new file mode 100644 index 000000000..d1c6f8163 --- /dev/null +++ b/day_6/exercises/launch_school_classes_define_objects.rb @@ -0,0 +1,66 @@ +=begin +class GoodDog +end + +sparky = 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!") + + +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 +=end + +# Exercises +# 1 +class MyClass +end + +myObj = MyClass.new + +# 2 +# A module is an ability to recycle groups of code. We must utilize +# the include method to "fit" our module inside of our classes. + +module Study +end + +class MyClass + include Study +end + +my_obj = MyClass.new diff --git a/day_6/exercises/launch_school_what_are_objects.rb b/day_6/exercises/launch_school_what_are_objects.rb new file mode 100644 index 000000000..cd6ecde34 --- /dev/null +++ b/day_6/exercises/launch_school_what_are_objects.rb @@ -0,0 +1,22 @@ +"hello".class +"world".class + +# Exercises +# 1 +class MyClass +end + +myObj = MyClass.new + +# 2 +# A module is an ability to recycle groups of code. We must utilize +# the include method to "fit" our module inside of our classes. + +module Study +end + +class MyClass + include Study +end + +my_obj = MyClass.new diff --git a/day_6/exercises/person.rb b/day_6/exercises/person.rb index 2c26e9570..5aec83c01 100644 --- a/day_6/exercises/person.rb +++ b/day_6/exercises/person.rb @@ -1,5 +1,54 @@ -# 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 MyPerson + attr_reader :hungry, :sitting, :standing, :tired + + def initialize(name) + @hungry = true + @sitting = false + @standing = false + @tired = true + + puts "Hi! My name is #{name}." + puts "I'm a human that's made up of only boolean values!" + puts "My boolean traits are currently:" + puts "Hungry = #{hungry}" + puts "Sitting = #{sitting}" + puts "Standing = #{standing}" + puts "Tired = #{tired}" + end + + def make_stand + @standing = true + end + + def make_sit + @sitting = true + end + + def eat + @hungry = false + end + + def sleep + @tired = false + end +end + +johnny = MyPerson.new("Johnny") + +p "Printing the result of asking Johnny to 'stand'...is Johnny Standing now?" +p johnny.make_stand + +p "Priting the result of asking Johnny to 'sit'...Is Johnny sitting now?" +p johnny.make_sit + +p "Printing the result of feeding Johnny...Is Johnny hungry now?" +p johnny.eat + +p "Printing the result of putting Johnny to bed...Is Johnny tired now?" +p johnny.sleep diff --git a/day_6/questions.md b/day_6/questions.md index f58ca5f71..b77eaa0f3 100644 --- a/day_6/questions.md +++ b/day_6/questions.md @@ -1,13 +1,51 @@ ## Day 6 Questions -1. In your own words, what is a Class? +1. In your own words, what is a Class? -1. What is an attribute of a Class? + A class is a way we can define a certain object. Objects can be anything that holds value. In theory, that's a lot of potential "stuff" out there! -1. What is behavior of a Class? + Out of all the potential and nearly infinite space of what we could define, classes are our specific specifications of what sort of values our objects will hold. That is, we've defined a few rules about what our class should be. + +1. What is an attribute of a Class? + + An attribute of a class is something that makes that class unique. Another way to think of an attribute is to think of characteristics of your defined class. + +1. What is behavior of a Class? + + A behavior of a class is what we might think of as a verb, or something involving action or *doing*. What are things we can have our class *do*? My class can have attributes, or descriptions, but without having my class actually do something, it's pretty much useless to me! 1. In the space below, create a Dog class with at least 2 attributes and 2 behaviors: +``` +class Dog + attr_reader :breed + attr_accessor :color, :name + + def initialize(name, breed, color) + @breed = breed + @name = name + @color = color -1. How do you create an instance of a class? + puts "The name of this dog is #{name}. Its color is #{color} and its breed is #{breed}" + end + + def dye_hair(color) + self.color = color + puts "Wow, your dog's hair color is now #{color}!" + end + + def change_name(name) + self.name = name + puts "Your dogs name is now #{name}." + end + end + + apple = Dog.new("Apple", "corgi", "black") + p apple.dye_hair('pink') + p apple.change_name("Cello") +``` +1. How do you create an instance of a class? +`set_variable = class.new()` 1. What questions do you still have about classes in Ruby? + + The differences between attr_reader and attr_accessor... From bbee11561f70b732057aa402fa57476408d4bfe9 Mon Sep 17 00:00:00 2001 From: ignored-comment Date: Mon, 14 Dec 2020 06:20:26 -0500 Subject: [PATCH 11/14] Initial Commit --- day_7/10_speckled_frogs.rb | 18 ++++++++++++++++++ day_7/fizzbuzz.rb | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 day_7/10_speckled_frogs.rb create mode 100644 day_7/fizzbuzz.rb diff --git a/day_7/10_speckled_frogs.rb b/day_7/10_speckled_frogs.rb new file mode 100644 index 000000000..9a91138e4 --- /dev/null +++ b/day_7/10_speckled_frogs.rb @@ -0,0 +1,18 @@ +def frog_poem(frogs) + while frogs > 1 do + puts "#{frogs} frogs sat on a log" + puts "eating some most delicious bugs." + puts "One jumped in the pool where its nice and cool," + frogs -= 1 + puts "then there were #{frogs} speckled frogs." + end + if frogs === 1 + puts "#{frogs} speckled frog 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 frogs!" + end +end + + +frog_poem(20) diff --git a/day_7/fizzbuzz.rb b/day_7/fizzbuzz.rb new file mode 100644 index 000000000..e0c125f8c --- /dev/null +++ b/day_7/fizzbuzz.rb @@ -0,0 +1,17 @@ +# FizzBuzz +def fizzbuzz (low, high) + while low <= high + if low % 15 === 0 + puts "FizzBuzz" + elsif low % 5 === 0 + puts "Buzz" + elsif low % 3 === 0 + puts "Fizz" + else + puts low + end + low += 1 + end +end + +fizzbuzz(25, 100) From e8c3ffa7d32ef5e2b2694e4fb9335d2fa8fe0ee8 Mon Sep 17 00:00:00 2001 From: ignored-comment Date: Mon, 14 Dec 2020 20:50:30 -0500 Subject: [PATCH 12/14] Add Day 7 Work --- day_7/high_level.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 day_7/high_level.md diff --git a/day_7/high_level.md b/day_7/high_level.md new file mode 100644 index 000000000..6c8975c42 --- /dev/null +++ b/day_7/high_level.md @@ -0,0 +1,28 @@ +# My Notes on the Caesar Cipher + +The Caesar Cipher is a means to encrypt a message. It takes the alphabet and your given number, and skews the alphabet N amount of times to the right in relation to our original alphabet. + +## Our pseudocode below: + +We can use a method to capture user data. + +Let's make a function that will take two parameters: the string that the user wants to encrypt, and the number of degrees we will shift our alphabet to the left. + +`def caesar_cipher(string, degrees)` + +Then we assign the variable `key` to the alphabet: + +`key = [ABCDEFGHIJKLMNOPQRSTUVWXYZ]` + +Then assign a blank array to the variable `cipher`. We push the results of our modified data at the variable `key` to our blank array at the variable ` +`cipher`. + +`cipher = []` + +Then we assign a blank string to be assigned to variable `encrypted`. + +`encrypted = ""` + +Now we loop through our variable `key` and, taking the `n` integer user inputs as an argument in the `degrees` parameter, and after shifting the elements at `key` n degrees to the left, pushes each modified character into our blank array held at the variable `cipher`. + +Once `cipher` is filled with the elements shifted `n` elements to the left from our `key`, we can now take our argument user includes in the parameter `string` and, comparing the changes from our `key` and `cipher`, output an encrypted message and push it to the `encrypted` variable. From a943fbac5475a12d2842c83223df506b746857b3 Mon Sep 17 00:00:00 2001 From: ignored-comment Date: Sat, 2 Jan 2021 20:06:33 -0500 Subject: [PATCH 13/14] Add Day 7 Work --- day_7/fizzbuzz.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/day_7/fizzbuzz.rb b/day_7/fizzbuzz.rb index e0c125f8c..b42bfcb35 100644 --- a/day_7/fizzbuzz.rb +++ b/day_7/fizzbuzz.rb @@ -1,17 +1,26 @@ # FizzBuzz def fizzbuzz (low, high) - while low <= high + while low < high if low % 15 === 0 - puts "FizzBuzz" + print "FizzBuzz, " elsif low % 5 === 0 - puts "Buzz" + print "Buzz, " elsif low % 3 === 0 - puts "Fizz" + print "Fizz, " else - puts low + print "#{low}, " end low += 1 end + if high % 15 === 0 + print "FizzBuzz" + elsif high % 5 === 0 + print "Buzz" + elsif high % 3 === 0 + print "Fizz" + else + print high + end end -fizzbuzz(25, 100) +fizzbuzz(1, 999) From 763f5458e4b5421b6795c81b5a2d542f15d221e4 Mon Sep 17 00:00:00 2001 From: ignored-comment Date: Sun, 3 Jan 2021 20:09:51 -0500 Subject: [PATCH 14/14] Add Day 2 Work --- day_2/exercises/iterations_and_each.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/day_2/exercises/iterations_and_each.rb b/day_2/exercises/iterations_and_each.rb index 699e72ebd..a3208d9aa 100644 --- a/day_2/exercises/iterations_and_each.rb +++ b/day_2/exercises/iterations_and_each.rb @@ -1,14 +1,11 @@ # Exercise 1: Doubles array_of_numbers = [1, 2, 3, 4, 5] array_of_numbers.each do |index| - puts index - puts index + 2.times {puts index} end # Triples array_of_numbers.each do |index| - puts index - puts index - puts index + 3.times {puts index} end # Exercise 2: Even #s only @@ -23,7 +20,9 @@ # Exercise 3: array_of_numbers.each do |index| - puts index * 2 + new = Array.new + new.push index * 2 + puts new end # Exercise 4: Full Name