% Introduction to Ruby
% Peter Jones <pjones@devalot.com>
%
Introduction
* Slide Time: 00:02 (00:02)
* Please ask questions at any time.
* Your job is to understand the material.
* My job is to keep us on track and on time.
30-second History of Ruby
- When and where?
- Influences.
- “Modern” Ruby.
* Slide Time: 00:02 (00:04)
* First released in 1995 by Japanese Programmer "Matz".
* Syntax influenced by Perl (C-style).
* Features influenced by Smalltalk and Lisp.
* Modern Ruby began with v1.9 (2007)
* Several Implementations, main is [MRI] [].
What is Ruby?
* Slide Time: 00:05 (00:09)
* Purely object-oriented.
* General purpose (not just web apps!)
* Dynamically typed.
* Garbage collected.
Ruby Syntax
* Slide Time: 00:08 (00:17)
* Perl-flavored, C-like syntax
* Semicolons are optional (line-based)
* Whitespace insignificant.
* Constants use CamelCase
* Variables and methods use snake_case.
* Blocks of code are delimited by `begin...end` or `do...end`.
* All methods return a value (no `void` functions).
* Implicit return of final value.
* The scope of a variable is determined by its name.
Everything is an Object
Consider this irb
session:
1.class
"Hello World".class
* Slide Time: 00:02 (00:19)
* Numeric literals are objects.
* String literals are objects.
Everything is an Object: Assignment
Open irb
and experiment with the following:
- Numeric literals.
- String literals.
true
and false
(0..10).class
* Slide Time: 00:06 (00:25)
Everything is an Expression
* Slide Time: 00:02 (00:27)
* An `if` statement is really an expression.
* Using `if` in string interpolation.
* Class definition is an expression.
* Method definition is an expression.
* Loading a file is an expression.
Everything is an Expression: Assignment
Open irb
and let’s play with some expressions together.
* Slide Time: 00:05 (00:32)
* `if true then "hello" end`
* `if false then "hello" end`
Object-oriented Programming
* Slide Time: 00:05 (00:37)
* Ruby only allows single inheritance.
* Multiple inheritance is emulated with modules.
* Modules *are not* interfaces.
Everything is an Object (Part 2)
Consider this irb
session:
String.class
anon = Class.new do
def hello; "hi"; end
end;nil
object = anon.new;nil
object.hello
* Slide Time: 00:05 (00:42)
* Even classes are objects!
* Creating classes at runtime.
Blocks and Iterators
* Slide Time: 00:03 (00:45)
* Blocks are borrowed from Smalltalk.
* Blocks can be used as iterators (`Array#each`).
Blocks and Iterators: Assignment
Open irb
and play with long- and short-form blocks.
* Slide Notes: 00:08:00 (00:53:00)
* Long form: `do |x| ... end`.
* Short form: `{|x| ... }`.
Blocks and Resource Management
* Slide Notes: 00:04 (00:57)
* And also for resource management (`File::open`).
* You can write methods which accept blocks.
* Blocks can be passed around from method to method.
Closing
Open irb
and ask some questions!
* Slide Time: 00:03 (01:00)