Dot Net Tricks

Articles about .NET, ASP.NET, C#, Object Oriented Programming and Agile Methodologies
Welcome to Dot Net Tricks Sign in | Join | Help
in Search

Software Theosophy

Initial Thoughts on Ruby on Rails

I did some Ruby about 6 months ago just to experiment with a non-Microsoft, non-strongly typed language.  I liked the syntax, but didn’t get deep enough with it to become overly excited.  It appeared to me to be just another scripting language (JASL?)  However, recently I picked up some Ruby on Rails and had to grudgingly admit I was impressed.  Here are some of my observations:

 

First, the MVC (model/view/controller) structure of Rails makes it conceptually similar to ASP.NET.  I’ve always liked the MVC pattern, back which I had investigated way back when I was still doing coldfusion and learning Java.  In Rails, the View is analogous to an .aspx page, and the Controller is similar to the code behind.  The bulk of your presentation code and html go into your view which is an .rhtml file.  However, the initial examples I messed with have some simple embedded Ruby code mixed in with the html in the typical classic ASP, JSP, PHP, fashion:

 

<table border="1">

 <tr>

  <td width="80%"><p align="center"><i><b>Recipe</b></i></td>

  <td width="20%"><p align="center"><i><b>Date</b></i></td>

 </tr>

 

<% for recipe in @recipes %>

 

  <tr>

   <td><%= link_to recipe.title, :action => "show", :id => recipe.id %></td>

   <td><%= recipe.date %> </td>

  </tr>

 <% end %>

</table>

 

For experienced ASP.NET developers who avoid using looping structures or other blocks of C# code directly in their html by using panels, repeaters and such this seems like sacrilege.  However what I’ve read so far suggests that you really shouldn’t put anything more complex than this in your .rhmtl “View” files, and that there is some other construct called “Builder Templates” that may offer an alternative.  Simple loops and expressions doesn’t seem much more complex than Repeaters or Labels.  In any case, the bulk of more complicated presentation/user interaction code rightfully belongs in the Controller, which is pure Ruby.  As I mentioned, this is fairly similar to the code-behind in ASP.NET.  Rails also seems to use some means of run-time code-generation or something akin to “partial classes” where instance variables and methods in the Controller are all accessible in the view.  Again, very similar to how code-behind works.

 

So I see a lot of similarity (although not sameness) between Rails and ASP.NET.  I always felt that ASP.NET had some parallels to MVC, even though it was not an exact implementation of the pattern. I really like this clean separation between the Html and the UI logic that is built into the Rails framework.

 

The other thing that I like is that Rails has Code-Generation and O/R Mapping built into the framework.  You point Rails to your database, run a command line utility and it creates your Model, which represents the business objects mapped to your database tables.  This again mimics stuff that we do in .NET all the time, except that we have to use third party tools such as Codesmith.  Relationship traversal did NOT seem automatic, so the tutorial has you add custom mapping code to the generated Model classes such as “belongs_to” and “has_many” that tells the type of relationship between the two clases:

 

class Recipe < ActiveRecord::Base

  belongs_to :category

end

 

class Category < ActiveRecord::Base

  has_many :recipe

end

 

 

Still, this is not a whole lot to write by hand.  I’m guessing all the fields and data access methods are hidden behind the scenes.  To grab some data and throw it on my view, I have the Controller ask these generated classes to get the data for me:

 

The controller would do ask the Model classes to get some data:

 

class RecipeController < ApplicationController

  scaffold :recipe

 

  def list

    @recipes = Recipe.find_all

  end

 

  def edit

    @recipe = Recipe.find(@params["id"])

    findcats

  end

 

  def findcats

    @categories = Category.find_all

  end

 

end

 

 

 

Also, the code generation extends all the way to the UI layer.  Rails will create Controllers and Views for you as well, so that you can get basic CRUD web pages generated for you and override them with custom code later.  This is great for getting some prototyping or basic admin tools done for a client. 

 

There are of course, huge differences.  As I alluded to above, Rails seems to encourage more of a classic ASP scripting style of coding such as embedding programming code in the Html and render blocks such as <%= %>.  This may seem a step backward to my .NET audience, but there are times when I feel nostalgia for this simpler time.  In ASP.NET, we have slightly cleaner separation, but we also have the plethora of events, methods, properties in the code behind that you have to be aware of in order to modify the markup such as Load, Init, ItemDatabound, ItemCreated as well as the complexity of traversing the control hiarchey using FindControl method or the Controls collection property.  I always felt that Microsoft made the page and control lifecycle overly complicated, especially to people learning ASP.NET.

 

Another difference is that the controller can manipulate multiple views.  So whereas in asp.net, one code-behind acts as the code for one .aspx page, the controllers in Rails can actually display or add functionality to multiple .rhtml pages.  I’ve always admired the class inheritance hierarchy in System.Web, where all pages inherit from a Page class and each page class has exactly one .aspx page hooked to it.  This seems to make sense and its consistent with both windows forms, “classic” VB6 and desktop apps written in Java.  Rails deviates from this a bit where multiple views/rhtml pages can be called by the same Ruby Controller class.

 

While I love the clean, English-like syntax of the Ruby language (a quality that makes me still appreciate Basic to this day) some thing have me baffled or outright concerned.  Ruby allows you to do some pretty crazy things if you come from a Java/C# perspective.  Ruby is dynamically typed, which means that it you lose a lot of the instant feedback from simple compile time errors such as misspelling the name of a variable or method, or attempting to cast something that you’re unable to cast.  There also is no intellisense in any of the Ruby IDEs that I have tried (FreeRIDE and RadRails). 

 

In addition to being dynamically typed, Ruby is a very dynamic language.  For instance, you can put an “if” statement right in the middle of a class definition.  This means that depending on things that happen at runtime, your class could have more or fewer methods, or different methods depending on what happens.  It’s sort of a Reflection.Emit type functionality built into the language without having to go thru a special namespace or API.  There are also ways to add functionality in such a way that it appears that you are altering the Ruby language.   The “belongs_to” declaration in the Model code above seems to fall in this category. This is very powerful, but like so many powerful features (such as multiple inheritance, which Ruby implements in a unique fashion called “Mixins”) it can easily be abused even by more senior programmers.   It means as a programmer in Ruby, you could potentially come across a lot more code that seems bizarre or completely alien to you, because the previous programmer is “changing the rules” at whim.  As I said to a colleague who was messing with Ruby, “When do you need the option to pass a Monkey object to a Car.Drive() method?”  There are some features that in my mind, while very useful in a small minority of situations, don’t seem very useful for most general purpose programming.  However I feel I should keep an open mind and I understand there are trade-offs between a dynamic language and a static language.  There’s no reason to open a religious war between the two philosophies on this blog.

 

So while some things always stay the same, Ruby has a lot of things that make it unique, for good or ill.  There’s a lot to love however, and Rails in particular seems pretty solid.  I think the jury is still out but I’m definitely going to keep an eye on this one, and try to finish this book.

 

Ruby on Rails:

http://www.rubyonrails.org/

 

Rails tutorial:

http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html

 

Ruby Mixins:

http://www.rubycentral.com/book/tut_modules.html

 

The book I’m reading: Agile Web Development with Rails

http://www.pragmaticprogrammer.com/titles/rails/index.html

Published Wednesday, September 06, 2006 12:55 PM by Fregas
Filed Under: , , , ,

Comments

 

Software Theosophy said:

Arbitrarily and without Robert H's consent, Andrew and i decided a couple of things the other day:We...
April 24, 2007 6:57 PM
Anonymous comments are disabled

About Fregas

Craig is currently the Lead Developer in Fort Worth, Texas for Enilon Group, a web development firm. He has been programming since 3rd grade (using the Commodoore PET) and professionally for the past 7 years. He has written several articles for ASPToday.com and co-authored the book "Beginning Web Programming using VB.NET and Visual Studio .NET" Currently, his favorite programming language is C#, but he has programmed in Visual Basic, T-SQL, Ruby, ColdFusion, ASP 3.0/VBScript, ASP.NET, Javascript, Java and even Pascal. Besides programming, Craig is best known for his cooking and his somewhat offbeat sense of humor.

This Blog

Post Calendar

<September 2006>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

Syndication

Powered by Community Server, by Telligent Systems