You are on page 1of 41

Lesson 1

Introduction to
Ruby

Saturday, September 11, 2010


Philosophy
behind Ruby

Saturday, September 11, 2010


Human
oriented

Saturday, September 11, 2010


Principle of
least surprise

Saturday, September 11, 2010


Fun to program in

Saturday, September 11, 2010


Features of Ruby

Saturday, September 11, 2010


Everything is an object
1.class # => Fixnum
'a'.class # => String
:z.class # => Symbol

class Person
end
Person.class # => Class
Person.new.class # => Person

Saturday, September 11, 2010


On the fly

def double_it(input)
input * 2
end
double_it(1) # => 2
double_it("a") # => "aa"

Saturday, September 11, 2010


Open for change
class Array
def first_half
slice(0, size/2)
end
end
a = [1, 2, 3, 4, 5, 6]
puts a.to_s #=> 123456
puts a.first_half.to_s #=> 123

Saturday, September 11, 2010


Program your programs

class Person
attr_accessor :first_name, :last_name
attr_reader :person_id
alias :last_name, :family_name
end

Saturday, September 11, 2010


Express yourself

me = people.find { |p| p.first_name ==


'Sau Sheong' }

family = people.select { |p|


p.last_name == 'Chang' }

Saturday, September 11, 2010


Perl
SmallTalk
+ Lisp
Ruby
Saturday, September 11, 2010
Current versions of
Ruby

Saturday, September 11, 2010


Matz Ruby Interpreter
Ruby 1.8.7

Saturday, September 11, 2010


Yet Another Ruby VM
-> Ruby 1.9.1

Saturday, September 11, 2010


JRuby (1.4)
Ruby 1.8 (1.9) on the JVM

Saturday, September 11, 2010


Rubinius
Ruby 1.8 in Ruby
(and a bit of C++)

Saturday, September 11, 2010


IronRuby (0.9.2)
Ruby 1.8

Saturday, September 11, 2010


MacRuby (0.6)
Ruby 1.9 in Objective-C

Saturday, September 11, 2010


Installing Ruby

Saturday, September 11, 2010


http://www.ruby-lang.org/en/downloads/

Windows - RubyInstaller

Mac - already pre-installed (1.8.7); or


use port

Linux (Debian, Ubuntu etc)

% sudo apt-get install ruby1.9.1-full

Saturday, September 11, 2010


Tools

Saturday, September 11, 2010


Rubygems

Saturday, September 11, 2010


Interactive Ruby
(irb)

Saturday, September 11, 2010


Language

Saturday, September 11, 2010


Strings and Variables

'Hello, Sau Sheong!'

name = 'Sau Sheong' # => "Sau Sheong"


"Hello, #{name}!" # => "Hello, Sau Sheong!"

Saturday, September 11, 2010


Methods
str = "Hello, World!" # => "Hello, World!"
str.size # => 13
str.sub("World", "Ruby") # => "Hello, Ruby!"

def greet(name="Sau Sheong")


"Greetings, #{name}!"
end

puts greet
puts greet('everyone')
# >> Greetings, Sau Sheong!
# >> Greetings, everyone!

Saturday, September 11, 2010


Numbers

1 + 2 # => 3
1 + 2.1 # => 3.1
2**10 # => 1024
9 / 2 # => 4
5 % 3 # => 2
-3.abs # => 3
1.class # => Fixnum
(2**50).class # => Bignum
1.3.class # => Float

Saturday, September 11, 2010


Arrays
Array.new # => []
[1,2,3] # => [1, 2, 3]
[1] + [2] << 3 # => [1, 2, 3]
[1,2,3] - [2,3] # => [1]
%w(one two) # => ["one", "two"]
[1,2,3].pop # => 3
[1,2].push(3) # => [1, 2, 3]
[1,2,3].shift # => 1
[2,3].unshift(1) # => [1, 2, 3]
[1,3].insert(1,2) # => [1, 2, 3]

Saturday, September 11, 2010


Hashes

options = {:name => "Ruby"}

h = Hash.new # => {}
h['string'] = 1
h[:symbol] = 1
h # => {:symbol=>1, "string"=>1}
h.keys # => [:symbol, "string"]
h[:symbol] # => 1

Saturday, September 11, 2010


Iterators

for n in 0..2
puts n
end

(0..2).each do |n|
puts n
end

[0,1,2].each {|n| puts n}

Saturday, September 11, 2010


Enumerable

ary = [1,2,3]
ary.map {|n| n * 2} # => [2, 4, 6]
ary.select {|n| n % 2 == 0} # => [2]
ary.inject(0) {|sum, n| sum + n} # => 6

Saturday, September 11, 2010


Control structures
if some_condition
# ...
elsif other_condition
# ...
else
# ...
end

while condition
# ...
end

puts "lol" if funny?

Saturday, September 11, 2010


Regular Expressions
str = "Hello, world!"
str.gsub(/[aeiou]/,"*") # => "H*ll*, w*rld!"
str =~ /w(or)ld/ # => 7
$1 # => "or"
match = str.match(/w(or)ld/) # => #<MatchData:
0x532cbc>
match[0] # => "world"
match[1] # => "or"

Saturday, September 11, 2010


Error handling
begin
raise "Standard error"
rescue => e
p e
end
# >> #<RuntimeError: Standard error>

begin
raise StandardError.new("Oh, oh")
rescue RuntimeError
puts "runtime"
rescue StandardError
puts "standard"
end
# >> standard

Saturday, September 11, 2010


Files

File.open('test.txt','w') do |file|
file.write("line\n")
file.puts("line")
end
File.read('test.txt')

Saturday, September 11, 2010


Classes
class Calc
attr_accessor :factor

def initialize(factor=2)
@factor = factor
end

def multiply(value)
@factor * value
end
end

calc = Calc.new
calc.multiply 5 # => 10
calc.factor = 3
calc.multiply 5 # => 15

Saturday, September 11, 2010


Inheritance
class Foo
def greet
"Hi, #{value}"
end
def value
"foo"
end
end

class Bar < Foo


def value
"bar"
end
end

Foo.new.greet # => "Hi, foo"


Bar.new.greet # => "Hi, bar"

Saturday, September 11, 2010


Class methods
class Foo
def self.bar
"BAR!"
end

def bar
"bar"
end
end

Foo.bar # => "BAR!"


Foo.new.bar # => "bar"

Saturday, September 11, 2010


Mixins
module Even
def even?
self % 2 == 0
end
end

class Fixnum
include Even
end

1.even? # => false


2.even? # => true

Saturday, September 11, 2010


Questions?

Saturday, September 11, 2010

You might also like