Ruby Crash Course

Girl Develop It

Instructor: Cecy Correa

Modified from GDI Seattle Ruby on Rails track.

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

Some "rules"

  • We are here for you!
  • Every question is important.
  • Help each other.
  • Have fun!

History of Ruby

  • Written / Invented by Yukihiro Matsumoto “Matz” in 1995
  • Gained popularity around 2000
  • Ruby on Rails comes out in 2005 and Ruby really explodes in popularity at that time.

Oh hai! I'm Matz!

Why Ruby?

  • It reads like English. The syntax is intuitive.
  • 
    # Java
    for(int i = 0; i < 5; i++)
    
    # Ruby
    5.times do
    			
  • Large, friendly community.
  • Lots of resources available!
  • You can get a web app off the ground quickly.
  • Using Ruby will cost you $0.
  • But also...

Was made for programmer happiness!

Meant to be very readable!

What is Ruby used for?

  • Web development (Rails)
  • iPhone apps (RubyMotion)
  • System Administration (Chef)
  • Testing (Vagrant)
  • Security (Metasploit)

Who is using Ruby?

  • Hulu
  • Funny or Die
  • Groupon
  • Shopify
  • Airbnb

Working with Ruby

For Ruby, we'll use REPL.IT to run Ruby code.

For Rails, we'll use CLOUD 9.

Setting up your own environment

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).

Super duper bonus points

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.

A quick word on programming...

When you're first learning

As you go on

Programming is...

  • 70% learning / researching
  • 10% actual coding
  • 20% debugging

Okay... I'm exaggerating!

Let's start coding!

https://repl.it

Select "Ruby"

Repl.It


				puts "Hello World!"
				

Does math!


				1 + 1
				

Will evaluate to 2!

You can subtract (-), multiply (*), "divide" (%)

We're coding!

Variables

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

Variables can store anything!

  • Words!
  • Numbers!
  • Randomly generated numbers!
  • The sum of things!

Can combine them with "puts"!

Let's check it out!

DEMO!

Your turn!

In Repl.It...

  • Use "puts" to return text
  • Do some math in Ruby
  • Set x = 1
  • Set y = 2
  • Sum x and y
  • Bonus points: set a variable to the sum of x and y!

Duration: 5 minutes

Ok, this is cool. But how do I get info from a user?

gets.chomp

Get a user's input


				gets.chomp
				

This grabs the info the user types in...

Can save this to a variable to get it again


				response = gets.chomp
				

Let's build a quick program using gets.chomp!

  1. We're going to get the user's name
  2. Get where they're from
  3. Get their favorite food

Let's build a quick program using 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
				

Let Ruby do the heavy lifting

Helper methods!

  • upcase
  • capitalize
  • swap case
  • reverse
  • length

These are all built-in with Ruby!

Demo

Can also write your own methods!

Make methods do things!

methods

They take something in and spit something out

Methods

This method takes in a name, and greets the name.


def greeting(name)
	name = name.capitalize
	puts "Hello " + name
end
				

This is pretty boring... let's do more!

Looping

Looping


10.times do
  puts "HELLO " + name
end
				

Beware of infinite loops!

DEMO!

Storing data!

  • Arrays
  • Hashes

Arrays

  • Stores things!
  • Numbers!
  • Strings!
  • Other arrays!

Arrays


my_fancy_array = [1, "two", "3", "A"]
				
  • Can call out the whole thing
  • Can call out individual parts

Creating a new array


my_fancy_array = []
				

But it's empty!

Putting stuff in an array


my_fancy_array << "stuff"
				

Putting stuff in an array (method #2!)


my_fancy_array.push("stuff")
				

How to take stuff out?

Arrays


my_fancy_array = [1, "two", "3", "A"]
				

There are different ways to take stuff out

.POP!


my_fancy_array.pop
				

This removes the last thing in the array

It also "returns" that value

Push and POP!

Getting something specific


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"

Demo!

Hashes

Hashes

Hashes allow you to store information in "key value" pairs.

Huh?

Hashes

Keys and Values go together. It helps you "link" data.

Think of it like an index, almost.

Hashes


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?

Let's use Hashes and Arrays to build a menu together

Hashes and Arrays


menu = {
	:drinks => ["OJ", "Coke", "Coffee"],
	:food => ["Burger", "Pizza", "PB&J"]
}
				

Hashes and Arrays

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!

More looping!

.each do

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!

Overwhelmed?
It's ok! We're almost there!

Classes

  • Object-oriented programming!
  • Modeled after “real” things
  • Have properties
  • Can inherit properties

Classes

Let's "model" a cat.

cat

A cat has...

  • a Name
  • a Breed
  • it Meows

DEMO!

Sample class


class Cat

	attr :name, :breed, :age

	def initialize(name, breed, age)
		@name = name
		@breed = breed
		@age = age
	end

	def meows
		puts "*meowwwww*"
	end

end
			

Class inheritance

cat and kitty

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.

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.

Inheritance travels down, not up.

Your turn!

  • Write a Class
  • Initialize it
  • Write a simple method for it (like meows)
  • Write a sub Class that inherits from your first Class
  • Keep it simple! :)

End of Day 1

Thank you!