Internet application development - Dariusz Harpula

Tuesday, February 27, 2007

Ruby, Ruby on Rails, PHP on Trax

[RUBY]

Ruby is a reflective, dynamic, 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 main implementation is free software. Ruby is said to follow the principle of least surprise (POLS), meaning that the language should behave in such a way as to minimize confusion for experienced users. Ruby is object-oriented: every bit of data is an object, even classes and types that many other languages designate as primitives (such as integers, booleans, and "nil"). Every function is a method. Named values (variables) always designate references to objects, not the objects themselves. Ruby supports inheritance with dynamic dispatch, mixins and singleton methods (belonging to, and defined for, a single instance rather than being defined on the class).
Though Ruby does not support multiple inheritance, classes can import modules as mixins. Procedural syntax is supported, but all methods defined outside of the scope of a particular object are actually methods of the Object class. Since this class is parent to every other class, the changes become visible to all classes and objects.

EXAMPLES:

Hello world example:

puts 'hello world'


Class example:

class Person
def initialize(name, age)
@name, @age = name, age
end

def <=>(person)
@age <=> person.age
end

def to_s
"#{@name} (#{@age})"
end

attr_reader :name, :age
end

group = [ Person.new("John", 20),
Person.new("Markus", 63),
Person.new("Ash", 16)
]

puts group.sort.reverse


Comparison between PHP and Ruby:

PHP Code:
$item) { echo "$index. $item"; }
?>


RUBY Code:
<% array = ['my list', 'of', 'items'] array.each_with_index { |item, index| p "#{index}. #{item}" } %>

[Ruby on Rails]

Ruby on Rails is a web application framework released in 2004 that aims to increase the speed and ease of web development. Often shortened to Rails, or RoR, it is an open source project written in the Ruby language. Like many contemporary web frameworks, Rails uses the Model-View-Controller (MVC) architecture for organizing applications. This pattern splits the view (also called the presentation) into "dumb" templates that are primarily responsible for inserting pre-built data in between HTML tags. The model contains the "smart" domain objects (such as Account, Product, Person, Post) that holds all the business logic and knows how to persist themselves to a database. The controller handles the incoming requests (such as Save New Account, Update Product, Show Post) by manipulating the model and directing data to the view. In Rails, the model is handled by what‘s called an object-relational mapping layer entitled Active Record. This layer allows you to present the data from database rows as objects and embellish these data objects with business logic
methods. The controller and view are handled by the Action Pack, which handles both layers by its two parts: Action View and Action Controller. These two layers are bundled in a single package due to their heavy interdependence. This is unlike the relationship between the Active Record and Action Pack that is much more separate. Each of these packages can be used independently outside of Rails.
Ruby on Rails strives for applications with less code, through guiding principles that include "Don't repeat yourself" (code should never be redundant) and "Convention over configuration" (programmers only need to specify when their setup differs from the norm). Rails provides out-of-the-box scaffolding, which can quickly construct most of the logic and views needed for a basic website, the WEBrick web server and other helpful development tools.

Instant Rails is a one-stop Rails runtime solution containing Ruby, Rails, Apache, and MySQL, all pre-configured and ready to run. It does not modify system environment. Current release of Instant Rails is for Windows, but there are plans for ports to Linux, BSD, and OSX.

[Php On Trax]

Php On Trax (formerly Php On Rails) is a web-application and persistance framework that is based on Ruby on Rails and includes everything needed to create database-backed web-applications according to the Model-View-Control pattern of separation.


PHP on Trax example:
title == '')
return array(false, "Title can't be empty");
if ($this->find_first("title = '{$this->title}'") != false)
return array(false, "Title must be unique");
return array(true);
}
public function validate_description() {
if ($this->description == '')
return array(false, "Description can't be empty");
return array(true);
}
public function salable_items() {
return $this->find_all("date_available <= now()", "date_available desc"); } } ?>


Ruby on Rails example:
<% class Product < message =""> "Title can't be empty"
validates_uniqueness_of :title, :message => "Title must be unique"
validates_presence_of :description, :message => "Description can't be blank"
def salable_items
Product.find(:all, :conditions => 'date_available <= now()', :order => 'date_available desc')
end
end
%>


References:
http://en.wikipedia.org/wiki/Ruby_programming_language
http://en.wikipedia.org/wiki/Ruby_on_Railss
http://api.rubyonrails.org/
http://www.phpontrax.com/

0 Comments:

Post a Comment

<< Home