![Page 1: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/1.jpg)
1
© 2002 IBM Corporation
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006
Building a Ruby on Rails application with DB2 Express-C 9
Introduction to RubyMarkham Oct 17, 2006
Antonio CangianoAlex PitigoiIBM Toronto Lab
![Page 2: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/2.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation2
Agenda
The basics of Ruby
Introducing the Ruby on Rails Web Framework
Building a Ruby on Rails application Part 1
Break
Building a Ruby on Rails application Part 2
![Page 3: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/3.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation3
What’s Ruby?
A programmer’s best friend
Ruby is a free and open source interpreted scripting language for quick and easy object-oriented programming
Released to the world in 1995 in Japan by Yukihiro Matsumoto, a.k.a matz
Stable version is 1.8.5
Used to build and program with the Ruby on Rails Web Framework
![Page 4: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/4.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation4
Main features 1/3
Purely object-oriented: everything is an object
Dynamically typed
Exception handling
Iterators and blocks
Native regular expressions
![Page 5: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/5.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation5
Main features 2/3
Principle of least surprise
Operator overloading
Automatic garbage collecting
Classes, methods, Modules, mixins
Highly portable
![Page 6: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/6.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation6
Main features 3/3
Large Standard Library
I18n support (UTF-8)
Continuations, generators, introspection, reflection and meta-programming
Extensible in C
![Page 7: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/7.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation7
The mandatory Hello World!
puts positions the cursor on the next line, print doesn’t
Single or double quotes delimit strings. Double quotes strings allow for special characters and variable substitution
![Page 8: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/8.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation8
IRB: Interactive Ruby Shell
irb is an interactive shell, excellent for trying out snippets of code, learning more about how Ruby works and cut down on the development cycle
![Page 9: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/9.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation9
RI: Ruby Interactive
![Page 10: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/10.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation10
Everything is an object… even numbers
![Page 11: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/11.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation11
Creating methods 1/2 Methods are created through the use of the def keyword
Methods start with a lowercase letter and should use the snake_case
The return keyword is optional, as the last evaluated expression is implicitly returned
![Page 12: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/12.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation12
Creating methods 2/2
Methods that answer a question, usually end with a ‘?’, while ‘destructive’ methods with a ‘!’
We said that numbers are objects… not special ‘primitive’ types. We can even define our own methods for them.
Arbitrary number of arguments, and default values are also allowed:
![Page 13: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/13.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation13
Access modifier 1/2
Methods are public by default
Access modifiers are methods that enable us to define the scope of other methods (public, private, protected)
Access modifiers continue to modify every following method within the current scope or until another access modifier is met.
Passing symbols to the access modifiers allows us to specify the methods independently from the order within the scope
![Page 14: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/14.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation14
Access modifier 2/2
![Page 15: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/15.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation15
Calling methods
We call methods through the dot ‘.’ operator: MyObject.my_method (sometimes dot is omitted because the caller is implicit). Methods can be concatenated: MyObject.my_method(a,b).another_method(d).another_one
Parenthesis after the method name are currently optional, but recommended in most cases
Calling a method means sending a message to the calling object: MyObject.my_method(a,b) is actually syntax sugar for MyObject.send(“my_method”,a,b)
![Page 16: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/16.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation16
Strings
![Page 17: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/17.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation17
Arrays
![Page 18: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/18.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation18
Hashes
Hashes are dictionaries composed of key/value pairs.
Hashes are associative Arrays that have keys as indexes.
The keys are often strings or symbols.
![Page 19: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/19.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation19
Regular Expressions
Using Regular Expressions
you can work on strings
through patterns
Regexp are efficiently
implemented in Ruby and
natively accessible.
Create Regexp with %r{}, / / or
Regexp.new()
![Page 20: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/20.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation20
Common Control Structures 1/3
Any expression, except false and nil, is evaluated as a true condition
|| and && are the logical OR and AND with short-circuit evaluation. (and, or, &, | are the always-evaluated versions)
Statement modifiers allow the usage of conditions after the actual statement, improving readability
![Page 21: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/21.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation21
Common Control structures 2/3
case uses the === operator to evaluate the condition. === is object specific and allow us to evaluate strings and regexp in the same case statement.
![Page 22: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/22.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation22
Common Control structures 3/3
![Page 23: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/23.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation23
Iterators and blocks 1/2
Blocks are nameless/anonymous functions that capture the container environment.
Variables used within a block needto be previously declared in orderto use their values outside the block.
Iterators are a convenient way foraccessing info stored in Array, Hashes,and any collections of data.
![Page 24: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/24.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation24
Iterators and blocks 2/2
![Page 25: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/25.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation25
Creating iterators
![Page 26: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/26.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation26
Naming conventions
local_variable
CONSTANT_NAME / ConstantName / Constant_Name
:symbol_name
@instance_variable
@@class_variable
$global_variable
ClassName
method_name
ModuleName
![Page 27: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/27.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation27
Classes 1/2
![Page 28: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/28.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation28
Classes 2/2
![Page 29: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/29.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation29
Accessors
![Page 30: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/30.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation30
Modules 1/2
Modules provide a namespace system
Modules allow the possibility to use mixins
![Page 31: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/31.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation31
Modules 2/2
![Page 32: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/32.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation32
Exceptions handling
Note: Check the documentation for raise, retry, catch and throw
![Page 33: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/33.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation33
Querying DB2
![Page 34: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/34.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation34
Querying DB2
![Page 35: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/35.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation35
Querying DB2 with parameterized queries
![Page 36: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/36.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation36
Calling DB2 Stored Procedures 1/2
![Page 37: Building a Ruby on Rails application with DB2 Express-C 9](https://reader036.vdocuments.us/reader036/viewer/2022062723/56813da6550346895da76eaf/html5/thumbnails/37.jpg)
IBM Information Management
IBM Center for Advanced Studies | CASCON 2006 | © 2006 IBM Corporation37
Calling DB2 Stored Procedures 2/2