# Features of Ruby used in Rails

**URL:** <https://www.ruby-forum.com/t/features-of-ruby-used-in-rails/49840>\
**Category:** Rails\
**Created:** [December 9, 2005, 5:22am UTC](https://www.ruby-forum.com/t/features-of-ruby-used-in-rails/49840 "2005-12-09T05:22:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishna.vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f19dbf/32.png) [@krishna.vivek](https://www.ruby-forum.com/u/krishna.vivek)\
**Post date:** [December 9, 2005, 5:22am UTC](https://www.ruby-forum.com/t/features-of-ruby-used-in-rails/49840/1 "2005-12-09T05:22:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krishna.vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f19dbf/32.png) [@krishna.vivek](https://www.ruby-forum.com/u/krishna.vivek)\
**Post date:** [December 9, 2005, 5:54am UTC](https://www.ruby-forum.com/t/features-of-ruby-used-in-rails/49840/2 "2005-12-09T05:54:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krishna.vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f19dbf/32.png) [@krishna.vivek](https://www.ruby-forum.com/u/krishna.vivek)\
**Post date:** [December 9, 2005, 6:03am UTC](https://www.ruby-forum.com/t/features-of-ruby-used-in-rails/49840/3 "2005-12-09T06:03:03Z")

</div>

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](http://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)

---

<div class="post-metadata">

**Author:** ![krishna.vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f19dbf/32.png) [@krishna.vivek](https://www.ruby-forum.com/u/krishna.vivek)\
**Post date:** [December 9, 2005, 6:15am UTC](https://www.ruby-forum.com/t/features-of-ruby-used-in-rails/49840/4 "2005-12-09T06:15:15Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![krishna.vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f19dbf/32.png) [@krishna.vivek](https://www.ruby-forum.com/u/krishna.vivek)\
**Post date:** [December 9, 2005, 6:39am UTC](https://www.ruby-forum.com/t/features-of-ruby-used-in-rails/49840/5 "2005-12-09T06:39:45Z")

</div>

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
