COMPARİSON OF RUBY ON RAİLS AND J2EE
Download
Report
Transcript COMPARİSON OF RUBY ON RAİLS AND J2EE
RUBY ON RAILS
•Caner
Çebi
•Emre Gürhan Işık
•H. Yılmaz Öztürk
•Hande İzmirlioğlu
•Özge Bekleyen
WHAT IS RUBY?
A dynamic, pure object oriented programming
language.
The code written in Ruby is not compiled into the
machine language, it is stored as text for the
Ruby interpreter, which takes this text and
interprets it to make the machine work.
HISTORY OF RUBY
In 1993 a japaneese guy Yukihiro Matsumoto
("Matz") started to work on Ruby.
He is inspired by Perl, Smalltalk and Lisp.
It is named as a gemstone because the creator is
inspired by Perl too much .
In 1993, Summer: First “Hello, world!” program
puts "Hello World!“
worked and its public release is in 1995.
2003, August 4: 1.8.0 is released.
2007,March:1.8.6 is released.
SOME DETAILS AND FEATURES
According to TIOBE index, it is ranked as 9th in
terms of its growth among other programming
languages. Beside being free of charge it is free to
use, copy, modify and distribute.
Everything including classes and types (integers,
booleans) are objects.
Flexible:
class Numeric
def minus(x)
self.-(x)
end
end
FEATURES OF RUBY CONT.
Inheritance with mixins, singleton methods,
dynamic dispatch.
Ex:
class MyArray
include Enumerable
end
Exception handling:
raise ArgumentError, "Illegal arguments!"
begin# Do somethingrescue# Handle
exceptionend
FEATURES OF RUBY CONT.
It has four levels of variable scope: local, global,
class and instance and scope is resoluted by $
and @.
Iterators and closures.
operator overloading.
has a support for introspection, reflection and
metaprogramming
It is highly portable
can load extension libraries dynamically.
automatic garbage collection for all Ruby objects
SAMPLE OF A CLASS
class Person
def initialize(name, age)
@name, @age = name, age
end
def <=>(person) # Comparison operator for sorting
@age <=> person.age
end
def to_s
"#@name (#@age)"
end
attr_reader :name, :age
end
group = [Person.new("Jon", 20),Person.new("Marcus", 63),
Person.new("Ash", 16) ]
puts group.sort.reverse
SAMPLE OF A CLASS
Output will be
Marcus (63)
Jon (20)
Ash (16)
CRITICISM
Among other dynamic scripting languages like
Perl, PHP and Pyhton its speed is much less.
It has some limitations on threading
method(green threads) it uses.
It doesnt have support for Unicode.
WHAT IS RUBY ON RAILS?
Ruby on Rails (RoR) is the super-productive new
way to develop full featured web applications.
Rails is the name of the web application
framework, and Ruby is the language used to
create web sites.
It creates skeleton applications that the
programmer can modify easily. So the duration of
creating complex web sites can be decreased by
using RoR.
PROPERTIES OF RAILS
“Convention over configuration (CoC).”
No configuration files.
“Don’t repeat yourself (DRY).”
The programmer does not need to specify the columns
of the database etc.
There is no need to write SQL queries.
It is similar to the J2EE architecture. It is one of
the most appropriate languages for Ajax.
INSTALLATION
To create web pages on Rails, the following
components should be installed:
Ruby
RubyGems: Standard Ruby package manager.
Rails: If RubyGems is already installed, Rails can be
installed with a command through command window.
MySql: For the database applications. (Other
databases can also be used, but some changes must
be made in the related files Rails creates.)
o RadRails is an eclipse plug-in, which can be used
to write web sites in Ruby.
Apache can also be used as web server in Rails
applications.
MODULES OF RAILS
These are the modules
installation period of Rails.
required
in
the
Active Record: Ruby’s approach to accessing data in
database. A database table is wrapped into a class
and used in the code.
Action Pack: Used to ease the web-request routing,
handling, and response.
Action Mailer: Used to simplify the creation of mail
service classes.
Active Support: Utility classes and extension to the
standard library that were required by Rails.
COMPARISON OF RUBY ON RAILS AND
J2EE
Figure of Rails and J2EE Stack.
COMPARISON OF RUBY ON RAILS AND
J2EE
Rails & J2EE both have a container in common in
which the application code will execute. (Webrick in
Rails, Tomcat Servlet Container in J2EE).
Struts' ActionServlet and Rails' DispatchServlet are
both examples of the Front Controller pattern; as such,
they both provide the same functionality(accept HTTP
requests, parse the URL, and forward processing of the
request to an appropriate action).
The main difference between the two front controllers
is how they determine the action that processes a
particular request.
COMPARISON OF RUBY ON RAILS AND
J2EE
Struts uses an XML file to externalize the mappings of specific
requests to Action classes.
Rails takes a different approach. Instead of relying upon a
configuration file to map requests to actions, it discovers the
appropriate action based on the URL requested.
Example:
COMPARISON OF RUBY ON RAILS AND
J2EE
In both Rails and Struts, the action acts as a bridge between the front
controller and the model. The developer provides an implementation of an
action in order to provide application-specific processing of a request. The
front controller is responsible for accepting the request and passing it off
to a specific action.
Rails and Struts action hierarchy:
COMPARISON OF RUBY ON RAILS AND
J2EE
Struts requires that the developer extend Action and
override execute() in order to process the request.
The front controller calls the execute() method and passes
it a number of useful objects, including the HTTP request
and response objects.
ActionForm is a class that conveniently transfers and
validates form-related input to and from the view, and
ActionMapping contains the configuration information for
the mapping as described in the XML.
The execute() method returns an ActionForward object that
Struts uses to determine the component that continues
processing the request. Generally, this component is a JSP,
but ActionForward can also point in other actions.
COMPARISON OF RUBY ON RAILS AND
J2EE
In Rails, you must extend ActionController::Base for the model to participate in the
processing of a request.
Developers need not be concerned with the threading issues that are present in
Struts, and as a result, the session, request, header, and parameters are all
accessible as instance members of the ActionController. ActionControllers are also a
logical place to group all processing of specific domain logic.
Extending ActionController::Base:
01 class OrderController < ActionController::Base
02
03
def list
@orders = Order.find_all
04
05
// Find all orders and set instance variable
// framework automatically forwards to list.rhtml
end
06
07
def delete
08
id = @params["id"]
09
Order.delete(id)
10
redirect_to :action => "list"
11
end
12 end
// Get the order id from the request
// Delete the order
// Forward to the list action (list method)
COMPARISON OF RUBY ON RAILS AND
J2EE
A persistence framework is used to move data to and from the
database in the application layer.
Both Hibernate and Rails persistence frameworks can be
classified as object/relational mapping (ORM) tools, meaning that
they take an object view of the data and map it to tables in a
relational database.
Hibernate is based on the Data Mapper pattern, where a specific
mapper class, the Session, is responsible for persisting and
retrieving data to and from the database.
Rails' ORM framework is called Active Record and is based upon
the design pattern of the same name.
COMPARISON OF RUBY ON RAILS AND
J2EE
Active Record doesn't require a mapping file, as Hibernate does; in fact, a
developer working with Active Record doesn't need to code getters or
setters, or even the properties of the class. Through some nifty lexical
analysis, Active Record is able to determine that the Order class will map
to the ORDERS table in the database.
The one line of code in the class body of Order defines its relationship to
the Item object. has_many is a static method call for which the symbol
:items is a parameter. ActiveRecord uses :items to discover the Item
domain object and in turn maps the Item object back to the ITEMS table
in the database.
01 class Order < ActiveRecord::Base
02
03 end
has_many :items
CREATING A PROJECT WITH RAILS
The creation of the skeleton of a web page using
Rails is made through the command line.
There are some key points for creating the page
and the database. They are the conventions,
which are necessary because they are used
instead of the configuration files (the CoC rule).
CREATING A PROJECT WITH RAILS
Go to a directory through the command line,
where you want to store your RoR project. Then
execute the following command:
rails project_name
With this command, Rails creates all the
necessary files and folders.
CREATED FILES AND FOLDERS
app: Contains the code of the application
models
views
controllers
helpers
components: Self-contained mini-applications that can
bundle together controllers, models, and views.
config: Contains configuration files for the Rails
environment (database, routing map, etc.).
db: Contains the database schema.
doc: Contains the documentation of the application.
lib: Initially empty. Used for the generic library files.
CREATED FILES AND FOLDERS
log: Contains the log files of the application. Divided
into four subfolders:
development
production
server
test.
public: Used for the non-ruby files like html files,
images, javascript files, css stylesheets, etc.
script: Helper scripts for automation and generation.
test: Contains test files.
vendor: Contains external libraries that the
application depends on
CREATING A PROJECT WITH RAILS
To see the project we have created in a browser,
we should run the server, which can be done with
this command:
ruby script/server
After this command, we can see our web site at
the address “localhost:3000”.
If we want to connect our web site to a database
and display the data in a page, we should obey
some rules while we create the tables in the
database and the corresponding pages.
CREATING A PROJECT WITH RAILS
In the database, the table names should be in
plural, and the pages we create for that tables
should be in the singular form of the same word.
The name of the table, which is used to store the
course list, should be “courses”, whereas the page we
want to create to list the courses should have the
name “course”, otherwise Rails warns you to name
the page and the table in an appropriate form.
In a table, if we make the first column “id”
(starting with small ‘i‘) and make it the primary
key, Rails generates the necessary code to access
the database automatically.
CREATING A PROJECT WITH RAILS
To create a page for the “courses” table in the
database, we should change the “database.yml”
file in the folder “config” and give the necessary
information (database name, host, etc.) to
automatically connect to our database by Rails.
After giving the database information to the
application, we can generate the skeleton of the
page by executing this command:
ruby script/generate scaffold course course
We give the singular form of the table’s name to
this command.
CREATING A PROJECT WITH RAILS
We can see the generated page in this address:
localhost:3000/course
This is the simplest form of listing a table’s
contents and editing or deleting them in a web
site.
If we check the directory “app/views/course” in
our project folder, we can see 5 different files
with the extension “rhtml”, which are “list, show,
new, edit, _form”.
CREATING A PROJECT WITH RAILS
“rhtml” is like jsp, where Ruby codes are scripted
into the html codes. So we can change the views
of automatically generated pages by modifying
these files.
list.rhtml:
It
is
the
page
we
see
at
“localhost:3000/course". It lists the rows of the table.
show.rhtml: It is the page to show the details of a
record in the database.
edit.rhtml: It is the page to edit a data in the
database.
new.rhtml: It is the page for adding a new record to
the database.
_form.rhtml: It is the common page of the “new” and
“edit” pages, because they have the same structure.
CREATING A PROJECT WITH RAILS
If we have two tables related to each other, we
can specify the relation between them by
modifying the files in the directory app/models.
In the models folder, there exist files, which have
the extension “.rb”, for each table in the
database. They are created after the scaffolding.
CREATING A PROJECT WITH RAILS
If we have a “students” table and it has a column
“courseId”, which we want to relate to the id
column of the “courses” table, we should make
following changes in these files:
student.rb:
belongs_to :course
course.rb:
has_many :students
Now we can access from one table’s record to the
other table’s records in the rhtml files.
We do not specify the relations in the database.
CREATING A PROJECT WITH RAILS
Displaying the content of one column in a drop
down list and taking the selected element is also
very easy in Rails.
If we want to select a course from the drop down list
and edit a student’s information or add a new student
according to that choice, changing the content of the
“_form.rhtml” file in the “app/views/student”
directory will be enoug. Because both “new” and
“edit” pages use that form as base, both of them will
be changed.
CREATING A PROJECT WITH RAILS
In the “_form.rhtml” file, all column names in the
database are represented as labels and below them,
the textboxes for the appropriate fields are set. If we
want to see a drop down list instead of a textbox, we
need to change this row
<%= text_field 'student', courseId' %></p>
Into this form:
<%= select("student", "courseId", Course.find(:all,
:order =>'name').collect {|c| [c.name, c.id]})
%></p>
CREATING A PROJECT WITH RAILS
Almost every web site has a left menu and a
header. If we want to do these with Rails, we
need to go to the folder “app/views/layouts”.
The folder contains “rhtml” files for each of the
pages, which are generated automatically when
the pages are generated. These files are html
files with Ruby codes. So we can design them
using html tags.
CREATING A PROJECT WITH RAILS
While these files are generated, <%= yield %> is
added to the file automatically. It represents the
actual code of the page, which is stored in the
views folder. If we design the page using html
tags, the page’s actual content will be set to the
place where we put this “<%= yield
%>”
command.
REFERENCES
http://www.rubyonrails.org/
http://rubyforge.org/
http://en.wikipedia.org/wiki/Ruby_on_Rails
http://www.yazilimgrubu.com/ruby-on-railsegiris-1/
http://www128.ibm.com/developerworks/linux/library/warubyonrails/?ca=dgr-lnxw16RubyAndJ2EE
Rails Cookbook, Rob Orsini, O’Reilly