An introduction to...

The Ruby Programming Language

logo

Saleem Ansari

twitter: tuxdna

22nd March, 2012 at Red Hat, Pune (India)

Presenter Notes

Outline

A bit of history

Ruby - getting started

Basic data types and variables

Basic IO

Control Structures

Regular Expressions

Methods ( or functions )

Object Oriented Programming

Modules and Mix-Ins

Gems

Additional Topics

Presenter Notes

Historical perspective on Ruby?

1991 - Linux kernel released ( by Linux Torvalds )

1991 - Python released ( by Guido van Rossum )

1995 - Java released (by James Gosling at Sun Microsystems )

1995 - Ruby language released (by Yukihiro Matsumoto / Matz )

logo

in the mean-time all the Ruby documentation was in Japanese only

2000 - the first english book publish - Programming Ruby

2004 - initial release of Ruby on Rails ( by David Heinemeier Hansson )

Currently, on github , Ruby (17%) is only second to Javascript (19%) in popularity, followed by Python (9%).

Presenter Notes

Ruby - getting started

Ruby - influenced by Python, Perl, Lisp, Smalltalk

Strongly Typed, Late Binding

Runs on VM, Garbage Collected, Object Oriented

Single Inheritance, Mxins

1 $ sudo yum install ruby ruby-irb rubygems # installation on Fedroa / RHEL
2 $ ruby my-script.rb      # running a ruby script
3 $ irb                    # interactive ruby
4 $ gem install wirble     # optional

Presenter Notes

Basic data types and variables

Variables - local, global, instance, class, constant

Numbers - Fixnum, Bignum

String - single-quote, double-qoute, delimeters, interpolation, here-doc

Shell Commands - back-quotes

Symbol

Range

Array

Hash

 1 name = "Matz"            # local variable
 2 age = 46                 # local variable
 3 $debug = true            # global variable
 4 @id = 10                 # instance variable
 5 @dept = "Engineering"    # instance variable
 6 @@total_instances = 10   # class variable
 7 MyClass                  # class name
 8 LOG_LEVEL = 10           # constant 
 9 var.object_id            # everything is an object
10 :north                   # symbol
11 [1,2,3,4]                # array
12 {:age => 46, :name => "matz" }     # hash

Presenter Notes

Basic Input / Output

1 puts LOG_LEVEL
2 print "Hello"
3 p "Object"
4 printf "My name is %s. I am %d years old.", 'matz', 46
5 line = gets

Presenter Notes

Control Structures

if / elsif / else / end

while / end

case / when / else / end

 1 grade = gets.chomp
 2 if grade == "A"
 3   "excellent"
 4 elsif grade == "B"
 5   "good"
 6 else
 7   "no one can save you now!"
 8 end
 9 # while loop
10 input = ""
11 while input != "quit"
12   input = gets.chomp
13   puts input
14 end
15 # statement modifier
16 puts "not a valid end of loop" if input != "quit"
17 puts "Never say die!" while true
18 # switch-case construct
19 grade = gets.chomp
20 case grade
21 when "A", "B"
22   puts "Pass"
23 else
24   puts "Fail"
25 end

Presenter Notes

Regular Expressions

1 /Perl (is|isn't) good/ # /regex/
2 r = Regexp.new("Perl (is|isn't) good")
3 str1 = "Perl is good"
4 str2 = "Perl isn't good"
5 str1 =~ /Perl (is|isn't) good/ # => 0
6 r.match(str2)      # => #<MatchData "Perl isn't good" 1:"isn't">
7 md = r.match(str2)
8 input = gets.chomp
9 puts "Matched" if input =~ /Perl (is|isn't) good/

Presenter Notes

Methods ( or functions )

Everything ( except Fixnum ) is pass-by-reference

 1 # check whether a string is a palindrome or not
 2 def palindrome?(s)
 3   a = s.gsub(/[\W]/, "").downcase
 4   return true if a.reverse == a
 5   return false
 6 end
 7 
 8 # factorial of a number
 9 def fact(number)
10   if number < 0
11      raise "Invalid input"
12   end
13   if number > 1
14     return number*fact(number-1)
15   else
16     return number
17   end
18 end

Presenter Notes

Object Oriented Programming

Class, instance variables

 1 class Person
 2    @@total_instances = 0
 3    def initialize(name, address)
 4      @name = name
 5      @address = address
 6      @@total_instances += 1
 7    end
 8    def say_hello
 9      printf "I am #{@name}, and I live at: #{@address}"
10    end
11    def Person.how_many
12      @@total_instances
13    end
14 end
15 class Employee < Person
16   def init
17 end

Presenter Notes

OOPs continued.

 1 class Employee < Person
 2   def initialize(name, address, company)
 3     super(name, address)
 4     @company = company
 5   end
 6   def say_hello
 7     super
 8     printf "I work at %s.\n", @company
 9   end
10 end
11 
12 p1 = Person.new("Saleem", "Magarpatta")
13 p2 = Person.new("Matz", "Japan")
14 p1.say_hello
15 p2.say_hello
16 printf "Persons instantiated so far: %d\n\n", Person.how_many
17 
18 e1 = Employee.new("Rambo", "Italy", "Hollywood")
19 e1.say_hello
20 printf "Persons instantiated so far: %d\n\n", Person.how_many

Presenter Notes

Modules and Mix-Ins

Module as a namespace

1 require 'person'
2 module College
3   class Student < Person
4   end
5 end
6 e4 = College::Student.new("Messi", "Argentina")
7 e4.say_hello
8 printf "Persons instantiated so far: %d\n\n", Person.how_many

Module as a mixin

1 class Person
2   include Comparable
3   def <=>(other) 
4     self.name.length <=> other.name.length
5   end
6 end

Presenter Notes

Ruby Gems

1 # gem install <gem-name>
2 # gem list

Presenter Notes

Additional Topics

Exception Handling

Blocks and Iterators

Proc, Lambda, Closure

Continuation

Duck Typing

Ruby one-liners

Presenter Notes

Questions?

IRC: #ruby

Presenter Notes

Thanks

Presenter Notes