the ruby programming language

35
The Ruby Programming Language with Ruby on Rails web application development

Upload: jana-valencia

Post on 03-Jan-2016

143 views

Category:

Documents


8 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Ruby Programming Language

The Ruby Programming Language

with Ruby on Rails web application development

Page 2: The Ruby Programming Language

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

Page 3: The Ruby Programming Language

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)

Page 4: The Ruby Programming Language

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.

Page 5: The Ruby Programming Language

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/rubyprint “Hello World\n”• Or in an object oriented manner.• Even full stack web applications

development environment, Rails.

Page 6: The Ruby Programming Language

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.

Page 7: The Ruby Programming Language

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.

Page 9: The Ruby Programming Language

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.

Page 10: The Ruby Programming Language

Ruby and her Ancestors

Page 11: The Ruby Programming Language

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.

Page 12: The Ruby Programming Language

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)

Page 13: The Ruby Programming Language

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

Page 14: The Ruby Programming Language

The Demonstration Environment

Page 15: The Ruby Programming Language

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.

Page 16: The Ruby Programming Language

“ri” example#ri StringA +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

Page 17: The Ruby Programming Language

Ruby Code examples

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

Page 18: The Ruby Programming Language

Some procedural code examples

• Example1.pl

• A ruby script uses the normal hash bang notation.

#!/usr/bin/ruby

print “Hello world\n”

Page 19: The Ruby Programming Language

String Manipulation

#!/usr/bin/ruby# example1.1# String manipulationparam1=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

closureparam1.split("").each do |letter| print "[" + letter + "]" + "\n"end

Page 20: The Ruby Programming Language

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

Page 21: The Ruby Programming Language

Example1.2.rb output

./example1.2.rb hello worldparam1=hello,param2=worldparam1=Olleh,param2=WORLD[h][e][l][l][o]

Page 22: The Ruby Programming Language

Rubys numeric classesFixnums and bignums

#!/usr/bin/ruby# Example1.3.rb# Fixnums automatically convert to Bignums#num=816.times do puts "#{num.class}: #{num}“ num *= numend

Page 23: The Ruby Programming Language

Output from example1.3.rb

Fixnum: 81Fixnum: 6561Fixnum: 43046721Bignum: 1853020188851841Bignum: 3433683820292512484657849089281Bignum: 11790184577738583171520872861412518665678211592275841109096961

Page 24: The Ruby Programming Language

Ruby Arrays and Hashes#!/usr/bin/ruby# Example1.1.rb Arrays and Hashes# Arrays can contain any number of any type of objecta=[ 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_send

# Some Examples of a Hashesmath_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 hashesa_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]}"

Page 25: The Ruby Programming Language

Example1.1.rb outputArray a's length is 5, and Class is ArrayThe Class of array a[0] = FloatThe Class of array a[1] = StringThe Class of array a[2] = FixnumThe Class of array a[3] = NilClassThe Class of array a[4] = ArrayThe value of pie is 3.14159a_hash has an object as a key, i.e. a_hash[b]=the b array

Page 26: The Ruby Programming Language

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

Page 27: The Ruby Programming Language

Active Record

• Example of an Active record class#!/usr/bin/rubyrequire ‘rubygems’require ‘activerecord’# Note: Convention over Configurationclass payment < ActiveRecord::Baseset_table_name “general_ledger“

enda_payment = Pament.find(12)a_payment.ammount_dollars = 30a_payment.save

Page 28: The Ruby Programming Language

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.

Page 29: The Ruby Programming Language

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

Page 30: The Ruby Programming Language

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

Page 31: The Ruby Programming Language

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

Page 32: The Ruby Programming Language

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

Page 33: The Ruby Programming Language

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

Page 34: The Ruby Programming Language

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

Page 35: The Ruby Programming Language

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’ homehttp://www.rubyonrails.org/