Last active
August 29, 2015 14:01
-
-
Save bryanwoods/a812a1935b6a91d83c43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In Ruby, your information (or data) can come in different types. | |
Let's learn about three to start: numbers, booleans, and strings. | |
Let's set a variable `my_num` to the value 25 | |
my_num = 25 | |
Let's set a variable `my_boolean` to the value true | |
my_boolean = true | |
Set `my_string` to "Ruby" | |
my_string = "Ruby" | |
One of the most basic concepts of programming is the variable. | |
You can think of a variable as a name for a value. | |
They're called variables because their values can vary. :) | |
my_num = 25 | |
my_num = 30 | |
my_num | |
Math | |
2 * 3 | |
10 + 5 | |
100 / 10 | |
10 ** 10 <-- Exponentiation, raising a number to a power | |
4 % 2 <-- Modulo, returns the remainder of division | |
puts and print | |
The print command take whatever you give it and prints it to the screen. | |
puts (for put string) is slightly different. It adds a new blank line after the | |
thing you want to print. | |
puts "What's up?" | |
print "Sup yo" | |
String Methods | |
Everything in Ruby has certain built-in abilities called methods. | |
You can think of methods as "skills" that certain things have. | |
For instance, strings (words or phrases) have built-in methods | |
that can tell you the length of the string, reverse the string, | |
and more. | |
"I love gin so much".length | |
"Bryan Woods".length | |
"Bryan".reverse | |
"bryan".upcase | |
"BRYAN".downcase | |
"Bryan".upcase.downcase | |
String Concatenation | |
"Bryan " + "Woods" | |
String interpolation | |
"Hi my name is #{name}" | |
Comments | |
# I am a full line comment! | |
"Bryan".length # I'm a comment too! | |
Naming Conventions | |
Local variables | |
Lowercase, snake case | |
my_name = "Bryan" | |
Exercise! | |
Create a variable called my_name and set it equal to your name as a string. | |
Create a variable called my_age and set it equal to your age as a number. | |
Math | |
Create a variable called sum that is equal to 13 + 379 | |
Create a variable called product that is equal to 923 * 15 | |
Create a variable called quotient that is equal to 13209 / 17 | |
Declare a variable name and set it equal to a string containing your name. | |
Call .downcase on your name to make it all lowercase, call .reverse on your | |
lowercase name to make it backwards, then call .upcase on your backwards name | |
to make it ALL CAPS. | |
You can do this in two ways. Each method call on a separate line, or you can | |
chain them together. | |
name.method1.method2.method3 | |
Take home assignment: | |
1. Write a program that prints the sum of the following two numbers: | |
30 to the power of four and 40 times eight. | |
2. Write a program that stores your name, age, and city in variables with those | |
names, and then prints an excited introduction with your name in all caps, | |
your age, your city in all caps, and ends with an exclamation point. | |
So for instance my program would set name to "Bryan Woods", age to 29, and | |
city to "Brooklyn" and print the following string: | |
"My name is BRYAN WOODS, I'm 29 years old, and live in BROOKLYN!" | |
3. Here is a link to the Ruby documentation for strings: | |
http://www.ruby-doc.org/core-2.1.1/String.html#method-i-5B-5D | |
Let's say I've go the string "Hello!" but have decided that it seems too exciting. | |
I want to chop off the last character at the end of the string. | |
Can you find a method that allows you to do this? | |
Ie: "Hello!".thismethod | |
should return "Hello" | |
Or "Kennethh".thismethod | |
should return "Kenneth" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# irb... | |
# Numbers | |
3 + 2 | |
# Strings | |
puts "Hi, everybody!" | |
# Booleans | |
true | |
false | |
nil | |
# Variables | |
x = 100 | |
puts x | |
# Math | |
# Addition (+) | |
# Subtraction (-) | |
# Multiplication (*) | |
# Division (/) | |
# Exponentiation (**) (raises one number to the power of the other) | |
# Modulo (%) (returns the remainder after division) | |
1 + 2 | |
3 - 5 | |
3 * 3 | |
9 / 3 | |
3 ** 2 | |
21 % 7 | |
# Puts and print | |
puts "Yo what's your name?" | |
print "Um...Bryan" | |
# Methods | |
"I love gin...gin gin gin".length | |
# You call methods with a `.`. | |
# `length` is a method all strings in Ruby have. It returns the string's | |
# length. | |
"Bryan".length | |
"Bryan".reverse | |
puts "bryan".upcase | |
puts "BRYAN".downcase | |
# Comments | |
# I'm a comment | |
3 + 2 # 5!! | |
# Local variables are lowercase and snakecase | |
name = "Bryan" | |
puts "Hi my name is #{name}" | |
my_name = "Bryan" | |
my_age = 29 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment