From 695b4c348f4fb5421800c523ac70d54a2335ea51 Mon Sep 17 00:00:00 2001 From: Kedasha Kerr Date: Wed, 2 Aug 2023 20:01:09 -0500 Subject: [PATCH 1/3] days 1 and 2 --- week-1/basics.rb | 149 +++++++++++++++++++++++++++++++++++++++++ week-1/control_flow.rb | 9 +++ 2 files changed, 158 insertions(+) create mode 100644 week-1/basics.rb create mode 100644 week-1/control_flow.rb diff --git a/week-1/basics.rb b/week-1/basics.rb new file mode 100644 index 0000000..d79ec82 --- /dev/null +++ b/week-1/basics.rb @@ -0,0 +1,149 @@ +=begin + +R U B Y ♦️ + +- Ruby is a dynamic, interpreted, object-oriented, high-level programming language that was created for developer happinness and was created in 1995 to emphasize the needs of humans over computer. +This makes the language reads like almost english. +- Interpreted means that you do not need a compiler to write or run Ruby, you can write it in your terminal and it will run. +- Ruby is case sensitive and cares about capitalizations +- to print variables with Ruby you use "puts" or "print" commands; the puts command adds a new line after the output, while the print command does not +- Single line comments are written with a hashtag (#) and multiline comments are written with =begin and =end + +D A T A T Y P E S & V A R I A B L E S + +- Numeric (integers, floats) +- Strings (text in strings) +- Booleans (true/false) +- Arrays (lists) +- Hashes (key/value pairs) +- Symbols (immutable strings) +- nil (nothingness) + +To write variables in Ruby, you need to use snake_case. +Eg: my_variable = 5 + + +A R I T H M E T I C O P E R A T O R S + +- Addition (+) +- Subtraction (-) +- Multiplication (*) +- Division (/) +- Exponentiation (**) - raises a number to the power of another number +- Modulo (%) - returns the remainder of a division + +EVERYTHING IN RUBY IS AN OBJECT + +- Ruby has built-in methods that can be called on objects +- Methods are called by appending the object with a dot (.) and the method name +- Methods can take arguments, which are passed in parentheses after the method name +- Methods can be chained together to do more complex operations + Eg: "hello".capitalize.reverse +- Methods can have an exclamation mark (!) at the end of their name to indicate that they modify the object they are called on + Eg: name = "ruby" + name.capitalize! + puts name + => Ruby + +- String Methods: + - .length : returns the length of a string + - .strip : returns a copy of the string with whitespace removed from the beginning and end + - .split : returns an array of substrings separated by a delimiter + - .start_with? : returns true if the string starts with one of the prefixes given + - .first : returns the first element of an array + - .last : returns the last element of an array + - .upcase : returns a copy of the string converted to uppercase + - .downcase : returns a copy of the string converted to lowercase + - .capitalize : returns a copy of the string with the first character converted to uppercase and the remainder to lowercase + - .reverse : returns a copy of the string with the characters in reverse order + - .include? : returns true if the string contains the given substring or character + - .empty? : returns true if the string is empty + - .nil? : returns true if the string is nil + + To print a string with a variable, you can use string interpolation: + Eg: name = "ruby" + puts "Hello #{name}!" + => Hello ruby! + +- Numeric Methods: + - .odd? + - .even? + - .round + - .abs + - .times + - .upto + - .floor + - .ceil + - .to_i + - .to_f + +- Getting input from users: + - gets is the Ruby method that retrieves input from users. + - "chomp" removes the extra line that is automatically added by the gets method + - gets.chomp : gets input from users and stores it in a variable + + +=end + +# Nums Boolean and String examples +my_num = 25 +my_int = 25.7 +my_boolean = true +my_string = "Ruby" + +puts my_num +puts my_boolean +puts my_string + +# Arithmetic Operators examples + +puts 5 + 5 +puts 5 - 5 +puts 5 * 5 +puts 5 / 5 +puts 5 ** 5 +puts 5 % 5 + +# String Methods examples + +puts "hello".capitalize +puts "hello".reverse +puts "hello".upcase +puts "hello".downcase +puts "hello".length +puts "hello".include?("h") +puts "hello".include?("z") +puts "hello".empty? +puts "hello".nil? +puts "hello".split +puts "hello".start_with?("h") +puts "hello".start_with?("z") +puts "hello".first +puts "hello".last +puts "hello".strip +puts name = "Hello".downcase.reverse.upcase #chaining methods + +# Numeric Methods examples + +puts 5.odd? +puts 5.even? +puts 5.round +puts 5.abs +puts 5.times +puts 5.upto +puts 5.floor +puts 5.ceil +puts 5.to_i +puts 5.to_f + +# Gettiing input from users + +puts "What is your name?" +name = gets.chomp # use gets.chomp to get input from users + store in a var +puts "Hello #{name}!" + +# modifying original var/object with ! +print "What city are you from?" +city = gets.chomp +city.capitalize! +puts "You are from #{city}!" diff --git a/week-1/control_flow.rb b/week-1/control_flow.rb new file mode 100644 index 0000000..ce8482b --- /dev/null +++ b/week-1/control_flow.rb @@ -0,0 +1,9 @@ +=begin + +C O N T R O L F L O W + + + + + +=end \ No newline at end of file From c0057292d9e986eb014ab6497afaf4295d4c6f54 Mon Sep 17 00:00:00 2001 From: Kedasha Kerr Date: Wed, 2 Aug 2023 21:56:44 -0500 Subject: [PATCH 2/3] installation docs --- week-1/basics.rb | 2 ++ week-1/installation.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 week-1/installation.rb diff --git a/week-1/basics.rb b/week-1/basics.rb index d79ec82..115dc3c 100644 --- a/week-1/basics.rb +++ b/week-1/basics.rb @@ -147,3 +147,5 @@ city = gets.chomp city.capitalize! puts "You are from #{city}!" + + diff --git a/week-1/installation.rb b/week-1/installation.rb new file mode 100644 index 0000000..bece204 --- /dev/null +++ b/week-1/installation.rb @@ -0,0 +1,33 @@ +=begin + +I N S T A L L A T I O N + +If you have a Mac, Ruby is automagically installed on your machine. Open your terminal and run the following command to check which version of Ruby you have installed: +`ruby -v` this should output the version of Ruby that you have. + +If you have a Windows machine, you can download Ruby from here: https://rubyinstaller.org/downloads/ and follow the instructions in this freecodecamp ruby video +- installation starts at (1:24) Windows Installation +- https://www.youtube.com/watch?v=t_ispmWmdjY&t=4095s + + +You may also want to install a Ruby Version Manager (RVM) to manage multiple versions of Ruby on your machine. +- I used this tutorial: https://nrogap.medium.com/install-rvm-in-macos-step-by-step-d3b3c236953b + + +For this course, you will need to have a Code Editor installed on your machine. I recommend using Visual Studio Code (VSCode) which you can download here: https://code.visualstudio.com/download +- With VS Code you can run ruby code directly in the editor, which is very convenient. +- I recommend adding the following extensions to your VS Code: + - Ruby https://marketplace.visualstudio.com/items?itemName=rebornix.Ruby + - VSCode Ruby https://marketplace.visualstudio.com/items?itemName=wingrunr21.vscode-ruby This extension will be installed automagically when you install the Ruby extension + +Running Ruby Code in VS Code + +- To run Ruby code in VSCode, open up a new file and save it with the .rb extension. +- To run the code, you can open up a new terminal in VSCode and run the following command: + - `ruby .rb` +- You can also run the code directly in the editor by pressing the play button in the top right corner of the editor window. + +=end + +# Example of Ruby Code +puts "Hello World!" \ No newline at end of file From 2afe0e63ae9526e3c2439bfc068d033afc1396fd Mon Sep 17 00:00:00 2001 From: Kedasha Kerr Date: Thu, 3 Aug 2023 21:54:55 -0500 Subject: [PATCH 3/3] study things --- week-1/basics.rb | 39 +++++++++++++++++++++++++++++---------- week-1/calculator.rb | 23 +++++++++++++++++++++++ week-1/madlibs.rb | 18 ++++++++++++++++++ 3 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 week-1/calculator.rb create mode 100644 week-1/madlibs.rb diff --git a/week-1/basics.rb b/week-1/basics.rb index 115dc3c..73054b6 100644 --- a/week-1/basics.rb +++ b/week-1/basics.rb @@ -59,23 +59,42 @@ - .include? : returns true if the string contains the given substring or character - .empty? : returns true if the string is empty - .nil? : returns true if the string is nil + - .index : returns the index of the first occurrence of the given substring or character To print a string with a variable, you can use string interpolation: Eg: name = "ruby" puts "Hello #{name}!" => Hello ruby! + ll stings start at index 0, and you can access individual characters using bracket notation: + Eg: name = "ruby" + puts name[0] + => r + + Ruby String methods cheatsheet: https://www.shortcutfoo.com/app/dojos/ruby-strings/cheatsheet + + - Numeric Methods: - - .odd? - - .even? - - .round - - .abs - - .times - - .upto - - .floor - - .ceil - - .to_i - - .to_f + - .odd? : returns true if the number is odd + - .even? : returns true if the number is even + - .round : rounds a float to the nearest integer + - .abs : returns the absolute value of a number + - .times : iterates block int times, passing in values from zero to int - 1 + - .upto : iterates block int times, passing in values from zero to int - 1 + - .floor : returns the largest integer less than or equal to float + - .ceil : returns the smallest integer greater than or equal to float + - .to_i : converts a number to an integer + - .to_f : converts a number to a float + - .to_s : converts a number to a string + + Numeric Class + - Math is a module that provides a lot of mathematical methods and constants. For example Math.log(1) => 0.0 + - there are a lot of math operations in the Math module/class + + When you add two integers you get an interget; when you add an integer and a float you get a float; when you add two floats you get a float. + Ruby distinguishes between integers and floats, and will convert between them as needed. + + More number methods: https://www.freecodecamp.org/news/ruby-number-methods-integer-and-float-methods-with-examples/ - Getting input from users: - gets is the Ruby method that retrieves input from users. diff --git a/week-1/calculator.rb b/week-1/calculator.rb new file mode 100644 index 0000000..9bc8853 --- /dev/null +++ b/week-1/calculator.rb @@ -0,0 +1,23 @@ +=begin +- To build a calculator we need to get input from the user, +- the inputs will be numbers and the operations to perform on those numbers +- perform some operations on the input and then output the result to the user +- we will use the gets method to get input from the user +- we will use the chomp method to remove the newline character from the input +- we will use the to_i method to convert the input to an integer +- we will use the puts method to output the result to the user +- be sure to handle float inputs as well as integer inputs + +=end + +# simple calculator to add 2 numbers +print "Enter a number: " +num1 = gets.chomp().to_i +print "Enter another number:" +num2 = gets.chomp().to_i + +print (num1 + num2) + +# create a more complex calculator that asks a user what operation they want to do with the numbers and do that operation with the numbers. +# is the user enters a word to represent the operation, we will need to convert that word to a symbol + diff --git a/week-1/madlibs.rb b/week-1/madlibs.rb new file mode 100644 index 0000000..5ba28ee --- /dev/null +++ b/week-1/madlibs.rb @@ -0,0 +1,18 @@ +=begin +Madlibs game is one where you ask the user for input, and then you plug that input into a story that you create. +=end + +# Simple Madlibs Game +puts "Enter a color: " +color = gets.chomp() +puts "Enter a plural noun: " +plural_noun = gets.chomp() +puts "Enter a celebrity: " +celebrity = gets.chomp() +puts "Enter a verb: " +verb = gets.chomp() + +puts ("Roses are " + color) +puts (plural_noun + " are blue") +puts ("I love " + celebrity) +puts ("So " + verb + " you are cool") \ No newline at end of file