The Ruby Programming language

Download Report

Transcript The Ruby Programming language

The Ruby Programming 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 Perl was first known as Perl 1.000

and in year Basic 1987 .

.

It became Perl 2.000

It became Perl 3.000

It became Perl 4.000

in year 1988 in year 1989 .

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

It became Perl 5.8.0

much since that time.

in year 2000 .

in year 2002 , and has not changed

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 Python was first known as Python and C .

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

It became Python 2.1

It became Python 2.2

It became Python 2.3

in year 2000 .

in year 2001 .

in year 2001 .

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 towards.

database tables.

principle of least surprise meaning that the language behaves in such a way as to minimize confusion for experienced users. • Ruby is designed for programmer productivity and 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 • 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 • But these can be overidden.

• DRY (Don’t Repeat yourself) (POLS), fun  . The

Installing ruby

>gunzip -dc ruby-1.8.5-p2.tar.gz | tar -xvf >cd ruby-1.8.5-p2 >vi README >export PATH=$PATH:/usr/ccs/bin >./configure --prefix=/dba/bin/ruby >make >make test >make install

The Demonstration Environment

Ruby Information ri and RDoc

• “ri” provides information on ruby classes • RDoc generates HTML and ruby information.

• “ri” displays information about classes • A browser can also be used to view generated HTML class documentation.

“ri” example

#ri String

A +String+ object holds and manipulates an arbitrary sequence of bytes, typically representing characters. String objects may be created using +String::new+ or as literals.

Because of aliasing issues, users of strings should be aware of the methods that modify the contents of a +String+ object. Typically, methods with names ending in ``!'' modify their receiver, while those without a ``!'' return a new +String+. However, there are exceptions, such as +String#[]=+.

----------------------------------------------------------------------- Includes: -------- Comparable(<, <=, ==, >, >=, between?), Enumerable(all?, any?, collect, detect, each_with_index, entries, find, find_all, grep, include?, inject, map, max, member?, min, partition, reject, select, sort, sort_by, to_a, to_set, zip) Class methods: new

Ruby Code examples

– Procedural Examples (example1.rb) – String Manipulation (example1.2.rb) – Bignums and Fixnums (example1.3.rb)

Some procedural code examples

• Example1.pl

• A ruby script uses the normal hash bang notation.

#!/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 puts "Array a's length is #{a.length}, and Class is #{a.class}" 5.times do | i | puts "The Class of array a[#{i}] = " + a[i].class.to_s

end # Some Examples of a Hashes math_values={'e' => 2.718128, 'pie' => 3.14159, 'gamma' => 0.83462} puts "The value of pie is #{math_values['pie'].to_s}" # even objects can be indexes to hashes a_hash={a => 'the a array', b => 'the b array', 'gamma' => 0.83462} puts “a_hash has an object as a kay, i.e.a_hash[b]=#{a_hash[b]}"

Example1.1.rb output

Array a's length is 5, and Class is Array The Class of array a[0] = Float The Class of array a[1] = String The Class of array a[2] = Fixnum The Class of array a[3] = NilClass The Class of array a[4] = Array The value of pie is 3.14159

a_hash has an object as a key, i.e. a_hash[b]=the b array

Ruby code examples

– – – – – – – – – – Class example (example2.rb) Adding a method (example3.rb) Class Variables (example4.rb) Inheritance (example5.rb) Closures (example6.rb) Closures with parameters (example7.rb) Practical uses of Closures (example8.rb) Exception processing (example10.rb) Connecting to Ingres (example11.rb) And next slide

Active Record

• Example of an Active record class

#!/usr/bin/ruby require ‘rubygems’ require ‘activerecord’ # Note: Convention over Configuration class payment < ActiveRecord::Base set_table_name “general_ledger“ end a_payment = Pament.find(12) a_payment.ammount_dollars = 30 a_payment.save

Other Members of the Ruby family

• Several other tools exists within the ruby/bin directory • ri – ruby information on classes, methods • rake – A make tool for ruby • gem - A Package management tool • irb - Interactive Ruby Shell • erb - Embedded Ruby scripts for Ruby active server type pages.

Installing “rubygems”

Ruby packages are generally distributed as gem files, i.e “activerecord-1.14.4.gem” except for “rubygems” which is the package management tool. “rubygems” can be installed using the following commands.

>cd /dba/bin/ruby/src/gems >tar -xvf rubygems-0.9.0.tar

>cd rubygems-0.9.0

>export PATH=$PATH:/dba/bin/ruby/bin >ruby setup.rb

Installing gems “ruby packages”

• If your machine is connected to the internet and not behind a firewall you may install ruby gems using the gem command, for instance, to install the package “activerecord”.

>gem install activerecord

• Or alternatively you can install gems from local files, “gems”

>gem install activerecord –-local

What is Rails

• Rails is a web application development framework, written entirely in ruby.

• Rails is packaged as a ruby gem package “rails-1.1.6.gem” • Once “ruby” and “rubygems” are installed rails can be installed using a single command, gem will get all of rails dependancies.

>gem install rails

Installing rails locally

# Installing rails locally from gems requires obtaining and installing all gems required by rails.

>cd ../extragems >gem install rake --local >gem install activesupport --local >gem install activerecord --local >gem install actionpack --local >gem install actionmailer --local >gem install actionwebservice --local

Installing Rails locally continued

# Go to the rails directory # i.e. where “rails-1.1.6.gem” is located >cd ../../rails # Install rails >gem install rails --local # Test rails if you want to >export PATH=$PATH:/dba/bin/ruby/bin >export MANPATH=$MANPATH:/dba/bin/ruby/share/man

A demonstration using rails

>rails fitness >cd fitness >cp ~/ingres_talk/fitness/database.yml config >cp ~/ingres_talk/fitness/create_fitness_schema.sql db >sql fitness < db/create_fitness_schema.sql

>ruby script/generate scaffold Member Members >ruby script/generate scaffold Payment Payments >ruby script/generate scaffold Fitness_class Fitness_classes >./script/server

Recommended reading, websites

• The Pragmatic Programmers Series.

• “Programming Ruby” “The Pickaxe book”, Dave Thomas • The Pickaxe book is also available online http://www.rubycentral.com/pickaxe/ (Some figures are missing) • “Agile Web Development with Rails”, Dave Thomas • Ruby online tutorial, via your web browser http://tryruby.hobix.com/ • Also O’Reilly’s “Ruby Cookbook”, good for getting ideas. • Ruby’s home http://www2.ruby-lang.org/en/ • Ruby on Rails’ home http://www.rubyonrails.org/