RUBY - 101
Lets learn Ruby
Hi, I’m Harisankar P S
Known in the world wide web as
@coderhs
Cochin, India
I program in Ruby and Rails for living :)
I am also the founder and CEO of
An exclusive Ruby based software
development company
Kerala Ruby User Group
@keralarb
http://krug.github.io
A group of people who loves working,
learning and talking about Ruby.
We have been active for the last three
years.
We meet every 3rd sunday
So thank you guys for hosting the
meetup for April, 2015.
With a bit of History
so lets begin
– Matz, the creator of Ruby
"Ruby makes programmers happy"
Yukihiro Matsumoto aka Matz
He created ruby in 1993
Released to the public is 1995.
First public version was 0.95
It was developed in Japan. 

First english mailing list was
created in 1999
Ruby is build to be as natural as possible.
So that we can avoid the above conversation
puts “Hello World”
Step 1 : Hello World
Numbers
2 + 2 #=> 4
2 + 3.15 #=> 5.14
2 ** 100 

#=> 1267650600228229401496703205376
# The largest number ruby can handle it the largest

# number your system can store
What about data types?
• Everything in Ruby is an Object
• 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']
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
list = (1..3)
squares = list.map { |i| i * 2 } #=> [1, 4, 9]
Enumerables
list = (1..3)
squares = list.map :square #=> [1, 4, 9]
Enumerables
list = (1..3)
squares = list.reduce :+
Enumerables
(1..5).select { |i| i%2 == 0 } #=> [2, 4]
(1..5).reject { |i| i%2 == 0 } #=> [1, 3, 5]
Enumerables
(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'
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'
Want to Learn More?
Websites
• rubymonk.com
• rubylearning.com
• tryruby.org
Books
• Why's Poignant Guide to Ruby
• Humble Little Ruby Book
credits
This presentation is built using information from
• Ruby 101 - Nithin Bekal (http://nithinbekal.com/slides/
ruby-101)
• Lets Learn Ruby (http://www.slideshare.net/
aquarianboy/lets-learn-ruby-basic)
• Ruby Basics (http://www.slideshare.net/
Robertdennyson/ruby-basics)

Ruby 101