More ECMA Awesomeness
May 22, 2007 @ 8:47 amI’ve been adding more tender loving javascript features to RKelly (my Javascript to Ruby converter). You can now call to_ruby on an RKelly object and get back your Javascript as Ruby. For example:
puts RKelly.process(<<END
function c() {
alert('asdfasdf');
}
var a = {};
foo['b'] = c;
END
).to_ruby
And that will out put the following ruby code:
def c
alert("asdfasdf")
end
a = lambda do
s = OpenStruct.new
return s
end.call
class << foo
def b
alert("asdfasdf")
end
end
Implicit object declaration is now supported too:
puts RKelly.process(<<END
var s = {
x: function () { alert("blh"); },
y: "foo"
};
END
).to_ruby
Which will output this:
s = lambda do
s = OpenStruct.new
class << s
def x
alert("blh")
end
end
s["y"] = "foo"
return s
end.call
One thing I’ve found while implementing RKelly is that javascript tends to pass around function pointers a lot. I will probably have to convert RKelly to declare lambdas for all functions instead of actual functions. The problem is that I’ll need to get it so the lambdas will be executed in the context of the object, but I’m sure that will be pretty easy.