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).