I am a fan of Ruby, and when I saw Red the framework that allows you to write Ruby and get JavaScript out the other end I was excited. It allows you to write this:

RUBY:

    class MyClass
       @@my_var = 500
       
       def initialize(arg)
         @arg = arg
       end

       def my_method
         alert(@arg)
       end
    end

And you end up with:

JAVASCRIPT:

  1.  
  2. 1| var MyClass = function(arg) { this.arg = arg;this.myMethod = function() { alert(this.arg); } }; MyClass.myVar = 500
  3.  

You can also have fun with blocks:

RUBY:

    [1,2,3].sort do |x,y|
      return y - x
    end
JAVASCRIPT:

  1.  
  2. [1, 2, 3].sort(function(x,y) { return y - x; })
  3.  

You also get convention conversion, which can lead to some weird things such as:

RUBY:

    Yahoo[:util]::Dom
JAVASCRIPT:

  1.  
  2. Yahoo.util.Dom
  3.  

On one hand I am excited about being able to think that I am hacking on Ruby, even though I am not. On the other hand, I worry about the abstraction leaking (ending with weird bugs) and how ruby is great because of all of the MOP, the libraries, and other things that you can’t really do with this system. This makes it a bit of a tease.

Source: Ajaxian » Front Page
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/363812875/prefer-ruby-syntax-see-red-and-your-ruby-will-convert-to-js

Comments are closed.



Site Navigation