Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

Ruby On Rails


Ruby Features

What is it about Rails that makes it the right tool for developing database-babysitting web apps?

At the top of the list has to be productivity. That was the tipping factor for Bruce Tate's customer, and it's surely the killer requirement for a web development tool, just as performance is the killer requirement for PC game development. Exactly how Rails makes you more productive comes down to a number of decisions that together make up a programming style. One of those decisions is the principle of Convention over Configuration.

Take plurals, for example. Rails automatically and dynamically fleshes out your Model objects based on your database, and does so with a sophisticated understanding of English plurals: A "customers" table will result in a Customer model, and a table named "people" will give you a Person model. You can override these decisions, but bowing to Rails' conventions saves a lot of coding.

One-stop shopping. Rails is a fully integrated web application framework. All you have to add to it are a web server and database. And the Model, View, and Controller components of your app are intelligently integrated without effort on your part.

Polish. Although Rails is a free, open application, it emerged into the wild only after serious real-world testing. Its creator David Hansson (see "A Conversation with David Hansson") used Rails to develop three successful web applications for his company. It's been out of beta since December, which technically makes it more mature than nine out of ten Google apps, snark snark.

Then there's Ruby, a language that combines the uncompromising object orientation of Smalltalk with the immediacy of Perl, Python, or PHP. Yukihiro "Matz" Matsumoto wanted to create a language that made programmers happy. Ruby is one of the most approachable languages we've ever seen, and Rails honors Ruby by trying to extend that approachability to the level of metaprogramming.

Among the features of Ruby that Rails makes powerful use of are:

  • Pure OOP. Everything, even an arbitrary real, is an object:

    
    irb(main):001:0> 2.2.round
    => 2
    
    

  • Reflection. You can ask Ruby objects about themselves:

    
    irb(main):002:0> 2.2.class
    => Float
    irb(main):003:0> 2.2.methods
    => ["%", "between?", "method", "send"
       , "prec", "modulo", "infinite?", 
       "object_id", "zero?", 
       "singleton_methods", "__send__", 
       "equal?", "taint", "*", "frozen?", 
       "instance_variable_get", "+", 
       "kind_of?", "step", "to_a", 
       "instance_eval", "-", "remainder", 
       "finite?", "prec_i", "nonzero?", 
       "/", "type", "protected_methods", 
       "extend", "floor", "eql?", 
       "display", "quo", 
       "instance_variable_set", "hash", 
       "is_a?", "to_s", "prec_f", "abs", 
       "singleton_method_added", "class",
       "tainted?", "coerce", 
       "private_methods", "ceil", 
       "untaint", "+@", "-@", "div", "id", 
       "**", "to_i", "<", "inspect", "<=>",
        "==", ">", "===", "clone", 
       "public_methods", "round", ">=", 
       "respond_to?", "freeze", "<=", 
       "nan?", "divmod", "to_f", "__id__", 
       "integer?", "=~", "methods", "nil?",
       "dup", "to_int", 
       "instance_variables", 
       "instance_of?", "truncate"]
    
    
  • Duck typing. Put simplistically, Ruby "ducks" the issue of typing, letting the type of a variable be determined by its value: "If it walks like a duck and talks like a duck, you might as well treat it as a duck."

    
    irb(main):004:0> v = 1
    => 1
    irb(main):005:0> v.class
    => Fixnum
    irb(main):006:0> v = "one"
    => "one"
    irb(main):007:0> v.class
    => String
    
    
  • Blocks. In Lisp, they're called "closures." Run this code in Ruby:
    
    puts "Here's what instances of String 
      can do:"
    String.instance_methods.each do 
      |method_name|
      puts method_name
    end
    
    

    and you'll see every string instance method (or operator).


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.