The Ruby Programming Language
The Ruby Programming Language
Language
with Ruby on Rails web
application development
Agenda
• Introduction
• The history of Ruby
• The philosophy of Ruby
• Installing Ruby
• The Demonstration Environment
• ri “Ruby Information”
• Ruby code examples
• Other Ruby tools
• Ruby Gems
• Rails
• Recommended reading and web sites
Introduction
What is Ruby ?
Ruby is a reflective (has the ability to observer and
modify its own behaviour), dynamic (compile and
runtime together), object-oriented programming
language. It combines syntax inspired by Perl
with Smalltalk-like object-oriented features, and
also shares some features with Python, Lisp,
Dylan, and CLU. Ruby is a single-pass
interpreted language. Its official implementation
is free software written in C. (Wikipedia)
The History of Ruby
• Ruby was written
by Yukihiro Matsumoto
• A Japanese computer
scientist and software
programmer, whose
hobbies include
computer programming
and compilier design.
Why is Ruby special
• Easy to use scripting language with a lot of
scope for deeper programming paradigms.
• For example Ruby can be used
procedurally.
#!/usr/bin/ruby
print “Hello World\n”
• Or in an object oriented manner.
• Even full stack web applications
development environment, Rails.
Perls Pedigree
• This is the genealogy of the programming language Perl:
• Perl is a child of awk, sh, C, csh, Pascal and Basic.
Perl was first known as Perl 1.000 in year 1987.
It became Perl 2.000 in year 1988.
It became Perl 3.000 in year 1989.
It became Perl 4.000 in year 1991.
Then it begat Ruby in year 1993.
It became Perl 5.000 in year 1994.
Then it begat PHP in year 1995.
It became Perl 5.005_50 in year 1998.
It became Perl 5.6.0 in year 2000.
It became Perl 5.8.0 in year 2002, and has not changed
much since that time.
Javas Pedigree
• This is the genealogy of the programming
language Java:
• Java is a child of C++ and Smalltalk.
Java was born in year 1991.
Then it begat NetRexx in year 1991.
It became Java 1 in year 1995.
It became Java 2 (v1.2) in year 1998.
Then it begat C# in year 2000.
It became Java 2 (v1.3) in year 2000, and has
not changed much since that time.
Pythons Pedigree
• This is the genealogy of the programming
language Python:
• Python is a child of ABC, Modula 3 and C.
Python was first known as Python in year 1991.
Then it begat Ruby in year 1993.
It became Python 1.5.2 in year 1999.
It became Python 1.6 in year 2000.
It became Python 2.0 in year 2000.
It became Python 2.1 in year 2001.
It became Python 2.2 in year 2001.
It became Python 2.3 in year 2003.
Rubys Pedigree
Ruby is a child of Python, Smalltalk, Eiffel
and Perl.
Ruby was born in year 1993.
It became Ruby 0.95 in year 1995.
It became Ruby 1.1 alpha 0 in year 1997.
It became Ruby 1.3.2 in year 1999.
It became Ruby 1.6.1 in year 2000, and
has not changed much since that time.
Ruby and her Ancestors
Ruby is still evolving
Ruby and Rails success’s have prompted others to
create similar languages such as “Groovy on
Grails” a Java like scripting environment, which
doesn’t seem to have had much success. In
addition another version of Ruby has been
written purely in Java which has access to all of
java’s native classes via an extension of the
ruby language, i.e access to JDBC. This is
variant is called jruby. A similar project has also
started at Q.U.T. to write Ruby using Microsofts
“.net” framework.
Ruby Philosphy
• Ruby is said to follow the principle of least surprise (POLS),
meaning that the language behaves in such a way as to minimize
confusion for experienced users.
• Ruby is designed for programmer productivity and fun . The
emphasis of ruby’s design is towards the convenience of the
programmer rather than optimizing the computer performance.
• Within the ruby community the objective of the programmers is
towards.
• Convention over configuration, there is a conventional way of doing
most things, i.e. MVC from rails. But there are also ways of
overriding the conventions through alternate configuration.
ActiveRecord uses pluralization rules, to associate class names with
database tables.
• But these can be overidden.
• DRY (Don’t Repeat yourself)
Installing ruby
#!/usr/bin/ruby
print “Hello world\n”
String Manipulation
#!/usr/bin/ruby
# example1.1
# String manipulation
param1=ARGV[0]
param2=ARGV[1]
print "param1=#{param1},param2=#{param2}\n"
print "param1=#{param1.reverse.capitalize}”
print “,param2=#{param2.upcase}\n"
# The unfamiliar format below is an example of a
closure
param1.split("").each do |letter|
print "[" + letter + "]" + "\n"
end
String Manipulation Continued
#!/usr/bin/ruby
# example1.2
# String manipulation
#
param1=ARGV[0]
param2=ARGV[1]
print "param1=#{param1},param2=#{param2}\n"
print "param1=#{param1.reverse.capitalize},param2=#{param2.upcase}\n"
#
# The unfamiliar format below is an example of a closure
#
param1.split("").each do |letter|
print "[" + letter + "]" + "\n"
end
Example1.2.rb output
./example1.2.rb hello world
param1=hello,param2=world
param1=Olleh,param2=WORLD
[h]
[e]
[l]
[l]
[o]
Rubys numeric classes
Fixnums and bignums
#!/usr/bin/ruby
# Example1.3.rb
# Fixnums automatically convert to Bignums
#
num=81
6.times do
puts "#{num.class}: #{num}“
num *= num
end
Output from example1.3.rb
Fixnum: 81
Fixnum: 6561
Fixnum: 43046721
Bignum: 1853020188851841
Bignum: 3433683820292512484657849089281
Bignum: 11790184577738583171520872861412518665678211592275841109096961
Ruby Arrays and Hashes
#!/usr/bin/ruby
# Example1.1.rb Arrays and Hashes
# Arrays can contain any number of any type of object
a=[ 3.14159, "pie", 99 ] ; b=[ 2.71828, "e", 100 ] ;a[4]=b
5.times do | i |
puts "The Class of array a[#{i}] = " + a[i].class.to_s
end
>./script/server
Recommended reading, websites