Wednesday, August 13th, 2008

Prefer Ruby syntax? See Red, and your Ruby will convert to JS

Category: JavaScript, Ruby

<>p>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:

  1. class MyClass
  2.    @@my_var = 500
  3.    
  4.    def initialize(arg)
  5.      @arg = arg
  6.    end
  7.  
  8.    def my_method
  9.      alert(@arg)
  10.    end
  11. end

And you end up with:

javascript
< view plain text >
  1. 1| var MyClass = function(arg) { this.arg = arg;this.myMethod = function() { alert(this.arg); } }; MyClass.myVar = 500

You can also have fun with blocks:

  1. [1,2,3].sort do |x,y|
  2.   return y - x
  3. end
javascript
< view plain text >
  1. [1, 2, 3].sort(function(x,y) { return y - x; })

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

  1. Yahoo[:util]::Dom
javascript
< view plain text >
  1. Yahoo.util.Dom

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.

Related Content:

Posted by Dion Almaer at 6:28 am
10 Comments

+++--
3 rating from 35 votes

10 Comments »

Comments feed TrackBack URI

Yuck — why would you ever want to write Ruby that produces JavaScript? You’ll always need to verify your code once it goes through the conversion magic, and you cannot optimize your final JS this way.

Can someone explain an advantage to this other than that it’s neat (which it is), or that it’s great for Ruby programmers who don’t know JavaScript?

Comment by MichaelThompson — August 13, 2008

I think Red, like Haml and Sass, isn’t specifically for programers who don’t know javascript. Like most abstraction languages it seems designed for people who do know the base language but dislike elements of its syntax.

Javascript is a little heavy on the extra characters compared to ruby or python. And people who write javascript as if it were a class based language (which is a fairly large group these days) have to churn out fairly odd looking syntax to get the behavior they want.

It’s easy to say “tough, that’s not what javascript is like” but unlike server side technologies (where I can dump php for python if the syntax bothers me or the language lacks features I want), javascript is foisted on the web community without choice. And if you think syntax and language features don’t matter to a programmer, go tell that to some ruby folks and watch then flip out!

Comment by trek — August 13, 2008

What I don’t understand is why people have a problem with the Javascript syntax. It’s got some of the best syntactic sugar of any language I’ve used, and can be some of the most concise code one can write. I’m increasingly of the opinion that people who don’t want to learn JS should stay away from writing JS.

Comment by eyelidlessness — August 13, 2008

@MichaelThompson: you could ask GWT the same question — why they would ever want to write Java that produces JavaScript. familiarity, Productivity, debugging tools, … yes, it’s great to grok JavaScript fully and completely, but that is not always for all folks. Nice to see alternate paths.

Comment by holts — August 13, 2008

Javascript is a little heavy on the extra characters compared to ruby or python.

Although in all the examples above the equivalent JavaScript code is less verbose than the Ruby code that produces it.

Heh.

Comment by waltr — August 13, 2008

Oh man. This is such a bad idea. One more layer of complexity. The worst part is, I’ve already started using it. I know I’m going to be sorry, but I cant help myself! It’s too awesome

Comment by railsjedi — August 13, 2008

trek had it dead on correct. This library is perfect for people who know javascript, and write a ton of it (like me) but still hate the syntax. And especially hate the context switching between ruby and javascript. I got on the RJS bandwagon for a while, but it was too messy.

It’s added work for sure. And the payoff is… being able to code more ruby I guess =)

Comment by railsjedi — August 13, 2008

@railsjedi: What don’t you like about JS syntax?

Comment by eyelidlessness — August 15, 2008

Red has gotten a significant update to version 4.0 that addresses some of the “it’s such a tease” feelings:

Red 4.0

Comment by trek — October 14, 2008

Nice article. I found another site showing ruby syntax in editor like style. http://www.hurricanesoftwares.com/meta-programming-with-ruby/
Although, the content is more into Meta programming but i liked the way they have displayed the Ruby Code.

Comment by riteshchopra — October 21, 2008

Leave a comment

You must be logged in to post a comment.