Forum: Ruby on Rails Features of Ruby used in Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 krishna.vivek (Guest)
on 2005-12-09 05:22
(Received via mailing list)
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
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 mpalmer (Guest)
on 2005-12-09 05:54
(Received via mailing list)
On Fri, Dec 09, 2005 at 09:51:43AM +0530, Vivek Krishna 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
D8cb8c8cd40ddf0cd05241443a591868?d=identicon&s=25 duane.johnson (Guest)
on 2005-12-09 06:03
(Received via mailing list)
On Dec 8, 2005, at 9:21 PM, Vivek Krishna 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 Johnson
(canadaduane)
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 krishna.vivek (Guest)
on 2005-12-09 06:15
(Received via mailing list)
>
> 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
E146c40bd510d1f1e455723485159670?d=identicon&s=25 zorander (Guest)
on 2005-12-09 06:39
(Received via mailing list)
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
This topic is locked and can not be replied to.