Modified from GDI Seattle Ruby on Rails track.
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
# Java
for(int i = 0; i < 5; i++)
# Ruby
5.times do
For Ruby, we'll use REPL.IT to run Ruby code.
For Rails, we'll use CLOUD 9.
We'll leave 30 minutes at the end of today to install Ruby and Rails, if you want to develop "locally" (i.e. saving and running files in your computer).
Want to 'deploy' your Rails site on the actual web? Sign up for HEROKU.
I will show you how to upload your Rails site on the web for those of you who are interested, but this is by no means required.
Okay... I'm exaggerating!
Select "Ruby"
puts "Hello World!"
1 + 1
Will evaluate to 2!
You can subtract (-), multiply (*), "divide" (%)
Just puting "Hello World!" every time is boring. We can store this in a variable!
Programmers are lazy. We like to type very little!
greeting = "Hello World!"
Variables can store anything!
Can combine them with "puts"!
In Repl.It...
Duration: 5 minutes
gets.chomp
This grabs the info the user types in...
Can save this to a variable to get it again
response = gets.chomp
puts "What is your name?"
name = gets.chomp
puts "Where are you from?"
location = gets.chomp
puts "What is your favorite food?"
food = gets.chomp
puts "Got it, your name is " + name
puts "You are from " + location
puts "And your favorite food is " + food
These are all built-in with Ruby!
Make methods do things!
They take something in and spit something out
This method takes in a name, and greets the name.
def greeting(name)
name = name.capitalize
puts "Hello " + name
end
10.times do
puts "HELLO " + name
end
my_fancy_array = [1, "two", "3", "A"]
my_fancy_array = []
But it's empty!
my_fancy_array << "stuff"
my_fancy_array.push("stuff")
my_fancy_array = [1, "two", "3", "A"]
There are different ways to take stuff out
my_fancy_array.pop
This removes the last thing in the array
It also "returns" that value
my_fancy_array[0]
my_fancy_array[1]
my_fancy_array[2]
You can get the value you want depending on where it is in the array
BEWARE! Arrays start at "0"
Hashes allow you to store information in "key value" pairs.
Keys and Values go together. It helps you "link" data.
Think of it like an index, almost.
menu = {:drink => "cola", :food => burger}
This helps keep info organized.
Everytime you want to look up what drink you have, "cola" will be returned
What are other things we could store in hashes?
menu = {
:drinks => ["OJ", "Coke", "Coffee"],
:food => ["Burger", "Pizza", "PB&J"]
}
Getting info out
menu[:drinks]
menu[:food]
Returns all drinks and food. To just get one, you have to use the item's position in the array:
menu[:drinks][0]
menu[:food][2]
So tedious!
You can use .each do to loop through items in an array!
menu[:drinks].each do |drink|
puts drink
end
This outputs all of the drinks at once!
Let's "model" a cat.
A cat has...
class Cat
attr :name, :breed, :age
def initialize(name, breed, age)
@name = name
@breed = breed
@age = age
end
def meows
puts "*meowwwww*"
end
end
What if you wanted to create a class "Kitty", but you knew it would be pretty similar to "Cat"?
Is there a way to "copy" a class?
Why, yes! (Sort of.) You can use sub-classes.
class Cat
#some stuff
end
class Kitty < Cat
#some more stuff
end
You can use the < to symbolize Kitty is a sub-class from Cat.
The stuff available for Cat is available for Kitty.
The stuff available for Kitty is NOT available for Cat.