Why Ruby?

Lutger Blijdestijn lutger.blijdestijn at gmail.com
Sun Dec 12 06:24:53 PST 2010


Jacob Carlborg wrote:

...
> 
> If we have a look at the next code example from the talk there is no
> clear and natural way to translate it into D, first the Ruby code:
> 
> class PeopleController < ActionController::Base
>      before_filter :authenticate
> 
>      def index
>          @people = Person.where("age > 30").order("name DESC")
> 
>          respond_to do |format|
>              format.html
>              format.xml { render :xml => @people.to_xml }
>          end
>      end
> end
> 
> And then the translated D code:
> 
> class PeopleController : ActionController.Base
> {
>      Person[] people;
> 
>      mixin before_filter!(authenticate);
> 
>      void index ()
>      {
>          people = Person.where("age > 30").order("name DESC");
> 
>          respond_to((Format format) {
>              format.html;
>              format.xml({
>                  render(["xml" : people.to_xml]);
>              });
>          });
>      }
> }

I agree with lambda syntax, iirc the trailing delegate was introduced around 
the time lazy made it into the language. However, D has some features of its 
own that could make for a better translation. 

In rails associative array literals are used as a workaround for the lack of 
named parameters in ruby. You can take advantage of overloading and template 
specialization in D to reduce the need for such a feature, which looks very 
strange anyway in D code. Furthermore, the code block syntax is used in ruby 
everywhere, but in D we also have nested functions, alias template 
parameters and lazy parameters. 

I'm sure a skilled designer could make a much better api than literally 
translating RoR. Perhaps something like this is already better:

class PeopleController 
    : ActionController!( before_filter!authenticate )
{
    People[] people;

    void index ()
    {
        people = Person.where("age > 30").order("name DESC");

        void respond(Format format)
        {
            format.html;
            format.xml( render(people.toXml) );
        }
        
        doResponse( &respond );
    }
}

One thing to consider is that a certain class of errors in D is caught by 
the type system. Even when you don't see static typing as an advantage 
perse, you can't ignore the fact that you have to write unittests for all 
these cases in a dynamic language whereas with static typing you save this 
need.


More information about the Digitalmars-d mailing list