Ruby 101

Hi, I'm Nithin

@nithinbekal


WowMakers / CrowdStudio

"Ruby makes
programmers happy"

Matz, the creator of Ruby

Hello, world!


puts 'Hello, world!'
	

A quick tour

Numbers


2 + 2      #=> 4
2 + 3.14   #=> 5.14
2 ** 100   #=> 1267650600228229401496703205376
	

What about data types?


2.class                #=> Fixnum
3.14.class             #=> Float
(2**100).class         #=> Bignum
	

Variables


x = 1        # x is an integer (class FixNum)
x = x + 1.5  # x is now a Float
x = 'Ruby'   # Now a string

puts "Hello, #{x}!"   #=> 'Hello, Ruby!'
  

Strings


'Hello, world!'
'Hello'.class   #=> String 
'Hello'.size    #=> 5
  

Arrays


items = [1, 2, 3]
items << 'a'       #=> [1, 2, 3, 'a']
  

Arrays


x = items.pop      #=> 'a'
items              #=> [1, 2, 3]
items.push 4       #=> [1, 2, 3, 4]
  

Ranges


(1..3)
(1..3).class  #=> Range
(1..3).to_a   #=> [1, 2, 3]
  

Hashes


person = {
  name:      'Matz',
  languages: ['C', 'Python', 'Ruby']
}

# OR

person = { 'Matz' }
person[:languages] = ['C', 'Python', 'Ruby']
  

Iteration


(0..2).each do |i|
  puts i*2
end
  

Conditions


if x == 2
  do_something()
end

do_something if x == 2
  

Conditions


do_something if x == true
do_something if x
  

Functions


def sqare(x)
  x*x
end

square(3)   #=> 9
  

Enumerables

Array of squares


list = (1..3)	

squares = list.map { |i| i * 2 }  #=> [1, 4, 9]
  

Enumerables

Using our square() method


list = (1..3)

squares = list.map :square   #=> [1, 4, 9]
  

Enumerables

Sum of the numbers


list = (1..3)

squares = list.reduce :+
  

Enumerables

Select/reject items


(1..5).select { |i| i%2 == 0 }  #=> [2, 4]
(1..5).reject { |i| i%2 == 0 }  #=> [1, 3, 5]
  

Enumerables

Or even shorter


(1..5).select :even?  #=> [2, 4]
(1..5).reject :even?  #=> [1, 3, 5]
  

Blocks


def foo
  puts 'Before yield'
  yield
  puts 'After yield'
end
	
foo { puts 'Inside the block' }

#=> Before yield
#=> Inside the block
#=> After yield
  

Procs


foo = Proc.new do
  puts 'Hello'
end

proc.call
  

Lambda


hello = lambda { |x| puts "Hello, #{x}!" }

hello.call 'world'   #=> Hello, world!
  

Objects and classes

Objects and classes


class Dog
  def greet
    'Woof!'
  end
end

dog = Dog.new
dog.greet       #=> 'Woof!'
  

Instance variables


class Dog
  def name=(n)
    @name = n
  end

  def greet
    "Woof, I'm #{@name}"
  end
end

dog = Dog.new
dog.name = 'Scooby'
dog.greet           #=> 'Woof! I'm Scooby.'
  

Constructors


class Dog
  def initialize(name)
    @name = name
  end

  def greet
    "Woof, I'm #{@name}"
  end
end

dog = Dog.new 'Scooby'
dog.greet                #=> 'Woof! I'm Scooby.'
  

Class variables


class Cat
  @@sound = 'Meow'

  def greet
    @sound
  end
end

garfield = Cat.new
garfield.greet       #=> 'Meow!'
  

Attribute accessors


class Dog
  def name=(name)
    @name = name
  end

  def name
    @name
  end
end
  

Attribute accessors


class Dog
  attr_accessor :name
end

# Also
# * attr_reader
# * attr_writer
  

Open classes


class Fixnum
  def plus_five
    self + 5
  end
end

1.plus_five   #=> 6
  

Inheritance


class Animal
  def initialize(name)
    @name = name
  end

  def greet
    'Hello!'
  end
end
  

Inheritance


class Dog < Animal
  def greet
    "Hello, I'm #{@name}! Woof, woof!"
  end
end

goofy = Dog.new('Goofy')
goofy.greet      #=> "Hello, I'm Goofy! Woof, woof!"
  

Inheritance


class Cat < Animal
  # do nothing here
end

tom = Dog.new('Tom')
tom.greet                #=> "Hello!"
  

Class methods


class Dog
  def self.default_greeting
    'Woof!'
  end
end

Dog.default_greeting    #=> 'Woof!'
  

Singletons


class Dog
  def greet
    'Woof'
  end
end

scooby = Dog.new
scooby.greet       #=> 'Woof'
  

Singletons


# ... contd

def scooby.greet
  'Scooby Doo!'
end

scooby.greet       #=> 'Scooby Doo!'
  

Modules - extend


module Speech
  def greeting
    'Hello'
  end
end

class Dog
  extend Speech
end

Dog.greeting      #=> 'Hello'
  

Modules - include


module Speech
  def greet
    'Hello'
  end
end

class Dog
  include Speech
end

dog = Dog.new
dog.greet      #=> 'Hello'
  

method_missing


class Dog
  def method_missing(method_name, *args)
    "Ï don't understand!"
  end
end

scooby = Dog.new
scooby.hello      #=> "I don't understand!"
  

method_missing


class Dog
  def method_missing(method_name, *args)
    if method_name.to_s =~ /^say_(.*)/
    	"Dog says: #{$1}"
    end
  end
end

scooby = Dog.new
scooby.hello      #=> nil
scooby.say_hello  #=> 'Dog says: hello'
  

Ecosystem

Implementations

MRI, Rubinius,

JRuby, MacRuby...

Gems

rubygems.org

RVM

rvm.io


Or Rbenv, chruby

Resources

Try Ruby

Run Ruby code in the browser


tryruby.org

Rubymonk

Interactive Ruby tutorials in the browser


rubymonk.com

Books


Why's Poignant Guide to Ruby

Learn to Program

Humble Little Ruby Book