Features of Ruby used in Rails


#1

Hi,
I have been using rails to do some pilot applications now and I have
often
come accross the comment that Rails uses Ruby features to the maximum. I
never could get what exactly are those things.However,one cool thing
that I
came accross was that new method names could be invented on models. Like

UserDetails.find_by_email_id’ .Although this method is not defined in
any
scaffold code generated ( correct me here,if needed)I figure that Rails
uses
the “method_missing” feature of Ruby to trap this method invocation and
somehow correctly generate an SQL query.
Is there more like this? Also do other languages like python or perl
not
support this kind of reflection?
Thanks
Vivek


#2

On Fri, Dec 09, 2005 at 09:51:43AM +0530, Vivek K. wrote:

Is there more like this? Also do other languages like python or perl not
support this kind of reflection?

Python supports trapping unknown method calls (via getattr); I’ve
not
worked out a way to make it look as neat as how it can be done in Ruby,
though.

  • Matt

#3

On Dec 8, 2005, at 9:21 PM, Vivek K. wrote:

Is there more like this? Also do other languages like python or
perl not support this kind of reflection?

I’m sure other languages do this.

Off the top of my head, here are some features:

  • Wrapping transactions in a block
  • The Routing code is a GeneratorGenerator which is pretty cool.
    Ruby code that creates ruby code… pretty deep stuff, though not yet
    Artificially Intelligent.
  • Mocks are a neat feature that take the place of Dependency Injection
  • Having converted ActiveRecord to PHP at one point (see
    phpontrax.com) I know that things like the acts_as_* genre of class
    methods are not possible in PHP. Generally, any class method called
    during the definition of a PHP class won’t work. Ruby is pretty
    amazing there.
  • Breakpoints are fabulous. They let you play with live objects,
    redefine classes on the fly etc.

Duane J.
(canadaduane)


#4

I’m sure other languages do this.

Off the top of my head, here are some features:

  • Wrapping transactions in a block

Is there any place I can get this? I mean i know i can always read
code…but still
I dont understand what transactions you mean here.

  • The Routing code is a GeneratorGenerator which is pretty cool.

Ruby code that creates ruby code… pretty deep stuff, though not yet
Artificially Intelligent.

Rails generates a lot of code but I have to agree the routing code 

and
logic works transparently It took me a while to figure out how those
urls
were converted to actions.
Thanks again
Vivek


#5

In python the metaprogramming is uglier but what you’d do is something
like this:

class whatever:
def getattr(self,name):
query_str = make_query_str(name)
class TempClass:
def call(): return do_sql_query(query_str)
return TempClass()

From the user’s perspective the result is the same, except that of
course function calls require () in python. I’ve used this technique
to build object proxies and it works out well in practice.

You can also accomplish this using metaclasses, but it would be more
complicated (though also more efficient…). Essentially, at class
creation time, you’d query the database (it would take a trick to
delay this like rails does, but tricks are easy when you can
access/implement namespaces via dictionary-like objects) and bind a
set of methods which instantiated objects of that type would use.

Vivek: Python is similarly flexible to ruby but a lot of this “slick”
stuff is more explicit and IMHO more complicated to implement (see:
metaclass). Multiple inheritance and explicit metaclasses are a
valid representation for the ideas that ruby solves with it’s
“metaclasses”, method_missing, modules, etc.

Brian