Download 6 Ruby in the Rough 7 Ruby on Rails

Document related concepts
no text concepts found
Transcript
Ruby
And
Ruby on Rails
Jim Fawcett
CSE686 – Internet Programming
Summer 2006
References





Brown-Bag Seminar Presentation, Beyond Java Chapters 4 and 5,
Jim Fawcett, Spring 2006
Brown-Bag Seminar Presentation, Beyond Java Chapters 6 and 7,
Taylan Yamliha, Spring 2006
Beyond Java, Bruce Tate, O’Reilly, 2005
Programming Ruby, Dave Thomas, PragmaticProgrammer, 2005
Agile Web Development with Rails, Dave Thomas, David Hansson,
PragmaticProgrammer, 2005
A Useful Definition for this
Presentation

Spin – from WordReference.com
• A distinctive interpretation (especially as
used by politicians to sway public
opinion); “the campaign put a favorable
spin on the story”.

When spun, your job is to:
• Listen, but be somewhat skeptical
• Make up your own mind
Some Spins



Bruce Tate - Author of “Beyond Java”
• Java is nifty, but growing too complex for a lot of the
jobs we need to do. Enter Ruby?
Kanat Bolazar– one of my Ph.D. candidate advisees
• Java is nifty, has lots of tools that let me do interesting
research. I love it! I’m skeptical of what the author
says.
Jim Fawcett - pragmatist
• Windows world is where most of the jobs are. There are
lots of interesting, but complicated, technologies to use
there. But windows development isn’t the only game in
town; Its just the one I have most interest in.
• I think the author is smart, has lots of interesting things
to say, and will give us a view into another world, if we
let him.
Chapter 4 – Glass Breaking

Java’s new job description
• Early use to develop quick applications
• Then moved server-side


Web site applications
Enterprise Systems
• Mapping objects into relational model
• Distributed transactions
• Messaging middleware
• Java is flexible, so its been able to
accommodate these changes.
Typical Requirements


Good fit to middleware development because of
its existing packages
Servlets and web programming is not as
productive as PHP
• “Java just isn’t very good for the simplest and most
typical applications.”


Better alternatives exist for XML processing.
Java’s strings are too verbose.
Natural fit to large enterprise projects due to its
libraries and large number of Java developers.
The Learning Curve



Most Java-based web applications use Java
Server Pages (JSP) technology, probably through
Tomcat.
There are more steps, now, in using Tomcat,
which take longer to learn.
But that’s not all. You’ll need to understand:
•
•
•
•
Ant – build and deploy web applications
Tapestry or Struts – organize user interface code
Hibernate – object relational mapper
Spring – organize application resources and
support testing
Resources

See the End Notes for summaries of:
•
•
•
•
•
•
•
•
•
PHP
JSP
J2EE
EJB
Apache
Tomcat
Struts
Tapestry
Spring Frmwrk
•
•
•
•
BeeHive
Hibernate
Ibatus
Maven
Hypertext PreProcessor
Java Server Pages
Java, Enterprise Edition
Enterprise Java Beans
Apache web server
Containers for Java Server Pages
Model-View-Controller application management
Simpler MVC application management
Framework for building containers of Java Server
Pages
Builds object model on top of J2EE and Struts
Object-Relational mapper
Data mapper framework
Manage build, reporting, and documentation
Java for the Typcial Application


For the basic problem, a web-based user
interface for a relational database, you
have to learn a lot more today than you
did five years ago.
The advanced frameworks will drive the
Java language away from the base that
made it popular
Agile Development

Simplicity
• Use the simplest thing that will work

Automated Unit Testing
• Junit framework is recommended

Shortened Iterations
• Shorter schedules, better integration of
customer feedback, and smaller
iterations
Development Processes & Java


Java’s community and tools provide
excellent support for agile
programming
Java is not such a good language for
agile development
• Not the simplest language
• Not friendly to very short iterations

Because of the need for frameworks
(implied)
Basic Java Limitations





Frameworks make experienced Java developers
more productive but make a steep learning curve
for new developers.
Compile-time type checking adds safety at the
cost of additional time and syntax.
Java is not able to express structured data leading
to over-dependence on XML.
Java’s many compromises, like primitives
(different than user types) make Java harder to
learn and more complex to write.
Java is nowhere near as dynamic as Smalltalk and
Ruby.
• Java’s long compile/deploy cycle is much longer than
interpreted, dynamic alternatives.
Alternatives

Are the Java community and large code base
worth sacrificing the productivity of other
alternatives?
• The waters are rising.



A clean, dynamic language could gain footing in
the gap between Visual Basic (6.0) and
Enterprise Java.
The lion’s share of Java development, even in the
enterprise, is not full of distributed transaction
and backbreaking loads.
Developers want a way to baby-sit a big
relational database with a web-based user
interface.
Chapter 5 – Rules of the Game

Portability
• Must have a virtual machine for:


Security, portability, extensibility, interoperability
Internet Focus
• The internet has two interfaces, for people and machines.
• The next language should build better interfaces more
easily, using a component model.
• It should deal with XML productively and efficiently.

Enterprise Integration
• The next language will should interoperate with the
existing code base.

Database Integration
• The language should access relational data in a natural,
productive way.

Transactions and Security
• Needs to integrate with existing enterprise security
frameworks.
Generating Buzz

Technically superior languages have failed because
they didn’t generate buzz.
• Walter Bright’s D is probably superior to C++, but it’s
generating little interest among the developer community.


Without buzz there is no community
Now is a good time for a new language:
• Open source software makes it easier for smaller companies to
become a force by virtue of numbers.
• Web services and other protocols make it easier to
interoperate between languages.
• A JVM (or CLR) can support other languages.

But generating buzz is more art than science, and
perhaps more luck than art.
Necessary Ingredients

Economics
• Someone has to pay the check, and superior productivity makes
that likely.

Approachability
• New language needs to grab new users quickly to prosper.

You should be able to get started quickly, and solve a problem that is
important to you quickly.
• C was approachable because you could solve low-level problems
with a high-level language.
• C++ was approachable because you could add C++ features to C,
as you needed them.
• Java was approachable because the syntax was familiar and it had
a clear path to internet applications.

The killer App
• A language needs an active community to prosper.
• A killer application can quickly draw new users.

Applets and servlets did that for Java.
Language Features

Dynamic typing
• Static typing promotes safety through compile-time checks, at the cost of
productivity.
• Dynamically typed languages are easier to explore and build prototypes.

Code blocks
• Code blocks are anonymous functions that are used to quickly assemble
callbacks, provide thread processing, and do repetitive operations on
databases and collections.
• They’re useful anywhere you need a “once-only” small function, and that’s
lots of places.
• They also reduce the remoteness factor.


The function body is defined right where it is used.
Continuations
• Saved stackframe hierarchies re-applied to go back to a prior state.
• Purported to be very useful for supporting backtracking in the stateless
web model.

Rapid feedback
• Reduce the time between making a change and seeing the result.
• The interpreted languages Smalltalk, Lisp, Perl, Ruby, and VB 6.0 all have
rapid feedback and are very productive languages.
More Language Features

User Interface Focus
• “User interface focus demands more than Java has to give”.

Dynamic Class Model
• A Java successor should be much more dynamic and reflexive.


Java’s reflection API is verbose, partly because of the distinction
between primitives and user types.
It has to deal differently with primitives, arrays, and classes.
• Tate gives two examples where a dozen lines of Java code are
replaced with one or two lines of Ruby code.
• With Ruby “you can also change classes, at runtime, on the fly.
You can change a method on an object and leave the class
untouched. Also, interceptions are childs play”.

Sound Foundations
• The next language should be object oriented and purer than
Java.

Here, the author means that primitives are first class objects.
• Consistency is important.

Languages with consistent naming and behavior are far easier to
learn.
Potential Suitors

Perl
• Productive scripting language
• Write only language with cryptic syntax

Python
• A successful dynamic programming language, close to Ruby in syntax
and power and supports the specified features.
• Syntax depends too much on white space.
• Has weak web development tools.

Ruby
• Object oriented dynamic language, created in Japan, and just now
becoming popular in US
• Beautiful syntax that stays out of your way.
• Highly dynamic
• Educated core of Ruby community works hard to produce clean, simple
APIs.
• Has strong web frameworks, good support for XML and web services.
• Ruby has a couple of high profile frameworks, like Ruby on Rails.
• Ruby has good commercial backing in Japan, but not in US.
• JVM support is immature, though improving rapidly.
More Suitors

PHP
• Scripting language.
• Start with html and mark up with tags for scripted actions with databases
and request and response objects.
• Easy to understand and learn.
• Good for controlling a database from a web page.
• Technically awful



Couples user interface and database together.
Method names are inconsistent.
C# and .Net Languages
• C# is a Java clone with many of the same benefits and drawbacks.
• Microsoft languages are a closed ecosystem. They succeed or fail with
the Microsoft platforms, rather than on their own merits.

Smalltalk
• Never caught on commercially, despite attempts by IBM as late as 1995.
• “Hughly productive, slightly awkward, and quirky to the extreme.”
• Clean object model, incredible expressive power, and an intelligent design
and community.
• Not seen as a credible alternative.
• “It just wasn’t ever approachable enough.”
A Random Quote from the Web

“I've been developing web apps for years, in both PHP and Java.
You really notice how much stuff is repeated and unnecessary. How
many times do you enter the same variable or field name in the
database, in the classes, in the HTML/view? It's a waste of time.
I had developed some helper utilities that are basically a crude form
of ActiveRecord (a Ruby on Rails construct).
I used to do a lot of Java development. J2EE [#@$%^&]. Servlets,
entity beans, etc. The amount of garbage you have to go through
just to get something deployed made me angry.
Seeing Rails was wonderful. It was all my "good ideas" implemented
cleanly with none of the PHP hacks or Java verbosity”.

Note:
There are many quotables about Ruby on Rails that are favorable,
and many that are not.
Next UP


Chapter 6 - Ruby in the Rough
Chapter 7 – Ruby on Rails
End Notes – Chapters 4 and 5





PHP, JSP, Java Beans, EJB, J2EE
Apache, Tomcat, Spring Framework
Struts, Tapestry
BeeHive, Hibernate, IBatus
Maven
PHP - http://us3.php.net/manual/en/introduction.php



(recursive acronym for "PHP: Hypertext Preprocessor") is a widelyused Open Source general-purpose scripting language that is
especially suited for Web development and can be embedded into
HTML.
Simple answer, but what does that mean? An example:
Example 1-1. An introductory example
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
JSP - http://www.webdevelopersjournal.com/articles/jsp_build.html

“If you've ever used Microsoft's very popular Active
Server Pages (ASP) then you'll have a good idea of
what JSP is. It consists of HTML or XML markup into
which special tags and code blocks are inserted. The
code is executed on the server and the result is a
dynamic page that is returned to the client browser.
Although JSPs are simple to build they have at their
disposal the full power of object-oriented Java and the
Java Server API. JSPs make heavy use of Java Beans,
which are classes that follow a standard pattern of a
no-argument constructor (required in JSPs) and public
GET and SET methods.”
Java Beans - http://en.wikipedia.org/wiki/Java_Beans





JavaBeans are software components written in the Java programming language.
The JavaBeans specification by Sun Microsystems defines them as "reusable
software components that can be manipulated visually in a builder tool".
In spite of many similarities, JavaBeans should not be confused with Enterprise
JavaBeans (EJB), a server-side component technology that is part of J2EE.
In order to function as a JavaBean class, an object class must obey certain
conventions about method naming, construction, and behavior. These conventions
make it possible to have tools that can use, reuse, replace, and connect
JavaBeans.
The required conventions are:
•
•
•
•

The class should be serializable (able to persistently save and restore its state)
It should have a no-argument constructor
Its properties should be accessed using get and set methods that follow a standard
naming convention
It should contain any required event-handling methods
Because these requirements are largely expressed as conventions rather than by
implementing interfaces, some developers view Java Beans as Plain Old Java
Objects that follow certain naming conventions. However, this view is misleading
for Java Beans that support event handling, because the method conventions and
associated support classes for event handling are fairly intricate, and require the
use of specific base classes and interfaces.
EJB - http://en.wikipedia.org/wiki/EJB


The Enterprise JavaBeans specification is one of the several Java APIs in
the Java 2 Platform, Enterprise Edition. EJB is a server-side component
that encapsulates the business logic of an application.
The specification details how an application server provides server-side
objects known as Enterprise JavaBeans, or EJBs, with:
•
•
•
•
•
•
•
•

persistence
transactions
concurrency control
events using Java Message Service
naming and directory services
security
deployment of components in an application server
remote communication using CORBA
Additionally, the Enterprise JavaBean specification defines the roles played
by the EJB container and the EJBs as well as how to deploy the EJBs in a
container.
J2EE - http://www.webopedia.com/TERM/J/J2EE.html

Short for Java 2 Platform Enterprise Edition. J2EE is a platformindependent, Java-centric environment from Sun for
developing, building and deploying Web-based enterprise
applications online. The J2EE platform consists of a set of
services, APIs, and protocols that provide the functionality for
developing multitiered, Web-based applications. Some of the
key features and services of J2EE:
• At the client tier, J2EE supports pure HTML, as well as Java
applets or applications. It relies on Java Server Pages and
servlet code to create HTML or other formatted data for the
client.
• Enterprise JavaBeans (EJBs) provide another layer where the
platform's logic is stored. An EJB server provides functions such
as threading, concurrency, security and memory management.
These services are transparent to the author.
• Java Database Connectivity (JDBC), which is the Java equivalent
to ODBC, is the standard interface for Java databases.
• The Java servlet API enhances consistency for developers
without requiring a graphical user interface.
J2EE - http://en.wikipedia.org/wiki/J2EE


Java Platform, Enterprise Edition or Java EE (formerly also
J2EE) is a programming platform – part of the Java platform – for
developing and running distributed multi-tier architecture
applications, based largely on modular components running on an
application server. The Java EE platform is defined by a
specification. Java EE is also considered informally to be a
language or standard because providers must agree to certain
conformance requirements in order to declare their products as
Java EE compliant; albeit with no ISO or ECMA standard.
Java EE includes several API specifications, such as JDBC, clientside applets, RPC, CORBA, and defines how to coordinate them.
Java EE also features some specifications unique to Java EE for
components. These include Enterprise Java Beans, servlets,
portlets (following the JSR 168 specification), JavaServer Pages
and several web service technologies. This allows the developer to
create an enterprise application that is portable between
platforms and scalable, while integrating with legacy technologies.
Apache - http://httpd.apache.org/



The Apache HTTP Server Project is an effort to develop
and maintain an open-source HTTP server for modern
operating systems including UNIX and Windows NT. The
goal of this project is to provide a secure, efficient and
extensible server that provides HTTP services in sync
with the current HTTP standards.
Apache has been the most popular web server on the
Internet since April 1996. The November 2005 Netcraft
Web Server Survey found that more than 70% of the
web sites on the Internet are using Apache, thus making
it more widely used than all other web servers
combined.
The Apache HTTP Server is a project of the Apache
Software Foundation.
Tomcat - http://java.sun.com/products/jsp/tomcat/


is a free, open-source implementation of Java
Servlet and JavaServer Pages technologies
developed under the Jakarta project at the
Apache Software Foundation.
Apache Tomcat is the servlet container that is
used in the official Reference Implementation
for the Java Servlet and JavaServer Pages
technologies.
Spring Framework






- http://www.springframework.org/about
The most complete lightweight container, providing centralized, automated configuration and wiring
of your application objects. The container is non-invasive, capable of assembling a complex system
from a set of loosely-coupled components (POJOs) in a consistent and transparent fashion. The
container brings agility and leverage, and improves application testability and scalability by allowing
software components to be first developed and tested in isolation, then scaled up for deployment in any
environment (J2SE or J2EE).
A common abstraction layer for transaction management, allowing for pluggable transaction
managers, and making it easy to demarcate transactions without dealing with low-level issues. Generic
strategies for JTA and a single JDBC DataSource are included. In contrast to plain JTA or EJB CMT,
Spring's transaction support is not tied to J2EE environments.
A JDBC abstraction layer that offers a meaningful exception hierarchy (no more pulling vendor codes
out of SQLException), simplifies error handling, and greatly reduces the amount of code you'll need to
write. You'll never need to write another finally block to use JDBC again. The JDBC-oriented exceptions
comply to Spring's generic DAO exception hierarchy.
Integration with Toplink, Hibernate, JDO, and iBATIS SQL Maps: in terms of resource holders,
DAO implementation support, and transaction strategies. First-class Hibernate support with lots of IoC
convenience features, addressing many typical Hibernate integration issues. All of these comply to
Spring's generic transaction and DAO exception hierarchies.
AOP functionality, fully integrated into Spring configuration management. You can AOP-enable any
object managed by Spring, adding aspects such as declarative transaction management. With Spring,
you can have declarative transaction management without EJB... even without JTA, if you're using a
single database in Tomcat or another web container without JTA support.
A flexible MVC web application framework, built on core Spring functionality. This framework is
highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP,
Velocity, Tiles, iText, and POI. Note that a Spring middle tier can easily be combined with a web tier
based on any other web MVC framework, like Struts, WebWork, or Tapestry.
Struts - http://struts.apache.org/


The goal of the Apache Struts project is to encourage
application architectures based on the "Model 2"
approach, a variation of the classic Model-ViewController (MVC) design paradigm. Under Model 2, a
servlet (or equivalent) manages business logic
execution, and presentation logic resides mainly in
server pages.
The Apache Struts project encourages Model 2
designs in two ways. First, by providing open source
frameworks and toolkits that help developers build
applications for the web. Second, by providing friendly
and honest mailing lists where both newcomers and
veterans discuss how to use Struts software in their
own Model 2 applications.
Tapestry - http://jakarta.apache.org/tapestry/index.html




Tapestry is an open-source framework for creating
dynamic, robust, highly scalable web applications in Java.
Tapestry complements and builds upon the standard Java
Servlet API, and so it works in any servlet container or
application server.
Tapestry divides a web application into a set of pages, each
constructed from components.
Developing Tapestry applications involves creating HTML
templates using plain HTML, and combining the templates
with small amounts of Java code using (optional) XML
descriptor files.
In Tapestry, you create your application in terms of objects,
and the methods and properties of those objects -- and
specifically not in terms of URLs and query parameters.
Tapestry brings true object oriented development to Java
web applications.
BeeHive - http://beehive.apache.org/

The Beehive goal is to make J2EE programming easier by building a
simple object model on J2EE and Struts. Using the new JSR-175
annotations, Beehive reduces the coding necessary for J2EE. The
initial Beehive project has three pieces.
• NetUI: An annotation-driven web application programming framework that
is built atop Struts. NetUI centralizes navigation logic, state, metadata, and
exception handling in a single encapsulated and reusable Page Flow
Controller class. In addition, NetUI provides a set of JSP tags for rendering
HTML / XHTML and higher-level UI constructs such as data grids and trees
and has first-class integration with JavaServer Faces and Struts.
• Controls: A lightweight, metadata-driven component framework for
building that reduces the complexity of being a client of enterprise
resources. Controls provide a unified client abstraction that can be
implemented to access a diverse set of enterprise resources using a single
configuration model.
• Web Service Metadata (WSM): An implementation of JSR 181 which
standardizes a simplified, annotation-driven model for building Java web
services.

In addition, Beehive includes a set of system controls that are
abstractions for low-level J2EE resource APIs such as EJB, JMS, JDBC,
and web services.
Hibernate - http://www.hibernate.org/



Hibernate is a powerful, high performance
object/relational persistence and query service.
Hibernate lets you develop persistent classes
following object-oriented idiom - including
association, inheritance, polymorphism,
composition, and collections.
Hibernate allows you to express queries in its
own portable SQL extension (HQL), as well as
in native SQL, or with an object-oriented
Criteria and Example API.
iBatis - http://ibatis.apache.org/


The iBATIS Data Mapper framework makes it
easier to use a database with Java and .NET
applications. iBATIS couples objects with stored
procedures or SQL statements using a XML
descriptor.
This framework maps classes to SQL
statements using a very simple XML
descriptor.
Maven - http://maven.apache.org/

Maven is a software project management
and comprehension tool. Based on the
concept of a project object model (POM),
Maven can manage a project's build,
reporting and documentation from a
central piece of information.
End of End Notes
– Chapters 4 and 5
Ruby



Dynamic
Fully object-oriented
Interpreted
OO

Everything is an object (ints, strings,
etc.)
irb(main):002:0>
=> 7
irb(main):003:0>
 Fixnum
irb(main):011:0>
=> 8
irb(main):012:0>
=> NilClass
7
7.class
7.5.round
nil.class
Typing

Dynamically typed
irb(main):013:0>
=> 1
irb(main):014:0>
=> Fixnum
irb(main):015:0>
=> "bbs"
irb(main):016:0>
=> String
n=1
n.class
n="bbs"
n.class
Typing

Strongly typed
irb(main):017:0> n+3
TypeError: cannot convert Fixnum into String
from (irb):17:in `+'
from (irb):17
Conditionals
irb(main):020:0>
irb(main):021:1>
irb(main):022:1>
=> nil
irb(main):023:0>
aaaa
=> nil
irb(main):025:0>
=> nil
irb(main):026:0>
aaaa
=> nil
irb(main):028:0>
=> nil
def silence?(b)
puts "aaaa" if b
end
silence? true
silence? false
silence? 0
silence? nil
Looping
irb(main):030:0> puts line while line = gets
"aaa"
"aaa"
one
one
two
two
three
three
^Z
=> nil
Ranges
irb(main):031:0>
=> 1..3
irb(main):032:0>
=> Range
irb(main):033:0>
=> true
irb(main):034:0>
=> false
irb(main):035:0>
=> true
range = 1..3
range.class
('a'..'z')==='h'
('a'..'z')==='H'
(1..10) === 5
Ranges
irb(main):036:0> for c in 'g'.. 'j'
irb(main):037:1> print c + '-'
irb(main):038:1> end
g-h-i-j-=> "g".."j"
irb(main):039:0> for c in 'g'.. 'j'
irb(main):040:1> puts c
irb(main):041:1> end
g
h
i
j
=> "g".."j"
Regular Expressions
irb(main):042:0>
=> /better/
irb(main):043:0>
=> Regexp
irb(main):044:0>
=> nil
irb(main):045:0>
=> 8
regex = /better/
regex.class
"Mine is bigger" =~ regex
"Mine is better" =~ regex
Containers - Hashes
irb(main):046:0> numbers={0=>"zero", 1=>"one",
2=>"two", 3=>"three"}
=> {0=>"zero", 1=>"one", 2=>"two", 3=>"three"}
irb(main):047:0> 4.times {|i| puts numbers[i]}
zero
one
two
three
=> 4
Containers - Arrays
irb(main):048:0> stack = [1,2,3]
=> [1, 2, 3]
irb(main):049:0> stack.push "cat"
=> [1, 2, 3, "cat"]
irb(main):050:0> stack.pop
=> "cat"
irb(main):051:0> stack
=> [1, 2, 3]
Files

A simple GREP
File.open(ARGV[0]) do |file|
rx = Regexp.new(ARGV[1])
while line=file.gets
puts line if line =~ rx
end
end
ruby grep.rb filename regex
Applying Some Structure


Inside-out Refactoring
Group common code in libraries
Call them in your methods
Outside in refactoring
Your methods can
take code-blocks as parameters
call them
even pass parameters to them
Outside-in Refactoring
def threeTimes
yield
yield
yield
end
threeTimes { puts "Hello" }
produces:
Hello
Hello
Hello
Classes
calculator.rb
class Calculator
def initialize
@total = 0
end
def add(x)
@total += x
end
def subtract(x)
@total -= x
end
end
Classes
irb(main):001:0> require calculator
NameError: undefined local variable or method
`calculator' for main:Object
from (irb):1
irb(main):002:0> require 'Calculator'
=> true
irb(main):003:0> c = Calculator.new
=> #<Calculator:0x2bd2500 @total=0>
irb(main):004:0> c.add 100
=> 100
irb(main):005:0> c.subtract 40
=> 60
Classes
irb(main):006:0>
irb(main):007:1>
irb(main):008:2>
irb(main):009:2>
irb(main):010:1>
=> nil
irb(main):011:0>
=> 0
class Calculator
def reset
@total = 0
end
end
c.reset
Classes
irb(main):012:0> class IrsCalculator < Calculator
irb(main):013:1> def add(x)
irb(main):014:2> x = x/2 if x>0
irb(main):015:2> super
irb(main):016:2> end
irb(main):017:1> end
=> nil
irb(main):018:0> c = IrsCalculator.new
=> #<IrsCalculator:0x2bba038 @total=0>
irb(main):019:0> c.add 100
=> 50
Classes
irb(main):020:0> Class.superclass
=> Module
irb(main):021:0> Module.superclass
=> Object
irb(main):022:0> Object.superclass
=> nil
Mixins




Interfaces with implementation
Can access the including class’s
methods
Partly adds the benefit of multiinheritance
Implemented using Modules
Interceptors

You can rename methods on-the-fly
class Class
alias_method :orig_new, :new
def new(*args)
result = orig_new(*args)
print “Unattended laptop error! ”
#puts result
return result
end
end
Interceptors
irb(main):001:0> require "Class"
=> true
irb(main):002:0> a=[1,2,3]
#<RubyToken::TkIDENTIFIER:0x2941d40>
#<RubyToken::TkASSIGN:0x2941b48>
#<RubyToken::TkLBRACK:0x2941950>
#<RubyToken::TkINTEGER:0x29416c8>
#<RubyToken::TkCOMMA:0x29414d0>
#<RubyToken::TkINTEGER:0x2941278>
#<RubyToken::TkCOMMA:0x29410b0>
#<RubyToken::TkINTEGER:0x2940e70>
#<RubyToken::TkRBRACK:0x2940c60>
#<RubyToken::TkNL:0x2940438>
=> [1, 2, 3]
Ruby On Rails


What is it?
“Rails is a full-stack, open-source web
framework in Ruby for writing real-world
applications with joy and less code than
most frameworks spend doing XML sit-ups”
David H. Hansson
Some Numbers

The author’s test case gives shows
impressively superior numbers for
Rails over Java/Spring/Hibernate
solution in development time/code
size as well as performance in most
cases.
Architecture
Under the Hood

Active Record
It’s a wrapper around a db table, with domain logic built
into the wrapper. You can also do inheritance and manage
relationships.

Action Pack
Splits the request into a controller part and a view part.
Showcases
RubyBB by Russ Smith
http://rubybb.readbim.com/
384 Lines of Code
StoryCards Web app to support XP style dev. by Jim Weirich
http://onestepback.org:3030/
1,250 Lines of code 8 hours of development time
Basecamp: A commercial Rails web-app with over 10,000
users.
“Web-based project management”
http://www.basecamphq.com/
Launched after 4,000 Lines of Code.
2 man-months of months of programming by a single
developer
End of Presentation