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.
Ruby is...
- Created for Programmer Happiness
- Object-Oriented
- Dynamic Type System
- Duck Type System
*Not* Static Type Language
Everything is True (except for False and Nil)
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 (%)
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!
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
Classes
- Object-oriented programming!
- Modeled after “real” things
- Self-contained
- Must be initaitied
- Can inherit properties from "parent" Classes
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
- 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...
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