Ruby Crash Course



Ruby Crash Course

0 0


intro-to-ruby

My slides for Intro to Ruby crash course, which I taught for The Iron Yard Austin

On Github cecyc / intro-to-ruby

Ruby Crash Course

Instructor Cecy Correa

History of Ruby

  • Written / Invented by Yukihiro Matsumoto “Matz” in 1995
  • Based on Lisp, Perl, and others
  • 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!

Ruby is...

  • Created for Programmer Happiness
  • Object-Oriented
  • Dynamic Type System
  • Duck Type System

Programmer Happiness

Object-Oriented

Dynamic Type Language

*Not* Static Type Language

Duck Typed

Everything is True (except for False and Nil)

Let's start coding!

https://repl.it

Select "Ruby"

Repl.It

				puts "Hello World!"
				
				print "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!

				greeting = "Hello World!"
				

Note the single =

single = means assignment

double = means evaluation / comparison

Variables

Variables can store anything!

  • String (text in "")
  • Integers (numbers without "")
  • Boolean values (true or false)
  • Random numbers!
  • The sum of things!

Helper methods!

  • upcase
  • capitalize
  • swap case
  • reverse
  • length

These are all built-in with Ruby!

DEMO!

Your turn!

In Repl.It...

  • Practice puts and print
  • Do some math in Ruby
  • Set x = a number
  • Set y = a different number
  • Sum x and y
  • Bonus points: set a variable to the sum of x and y

Duration: 5 minutes

Methods!

Methods do things!

They take in properties (arguments)

Methods

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

					def greeting(name)
	    		name = name.capitalize
	    		puts "Hello #{name}"
					end
				

Methods

Methods can do fancy things, like looping!

					def annoying_greeting(name, i)
					    name = name.upcase
					    i = i
					    i.times do
					        puts "HELLO #{name}"
					end
				

DEMO!

Classes

  • Object-oriented programming!
  • Modeled after “real” things
  • Self-contained
  • Must be initaitied
  • Can inherit properties from "parent" Classes

DEMO!

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
  • Start a new class in Repl and set it to a variable, try calling its methods

Exercise: Number Guessing Game

Rails!

Rails

  • Most well-known Ruby gem!
  • Invented by DHH (Basecamp)
  • Came out in 2005
  • Powers Twitter in 2006
  • Used by companies like Groupon, Funny or Die, Hulu, Airbnb...

Student projects

Other gems!

Alternative Ruby implementations

  • JRuby: Ruby that compiles to Java
  • Opal: Ruby that compiles to Javascript
  • Ruby Motion: Ruby that lets you code iOS and Android apps

Resources

Thank you!

Ruby Crash Course Instructor Cecy Correa