Premshree Pillai ([info]premshree) wrote,
@ 2005-05-30 10:57:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Current music:Motley Crue - Shout At The Devil

Mozilla Kinda Discovers Rubyland

"ruby javascript".split(" ").map (
	function (ele) {
		return ele.toUpperCase();
	}
)

I know, that easily looks like some Ruby code. But it’s goddamn JavaScript.

Wait, there’s more. Some day, not so far away, I must guess, we could soon be doing something like this:

["I", "love", "objects"].forEach (
	function (element, index, array) {
		print element;
	}
)

Basically, the cool folks at Mozilla have extended the JavaScript specifications, specifically adding a buncha cool stuff to the Array object.

You sure do see where this has probably come from, don’t you? Now if only it’d behave like real Ruby blocks.

(Discovered via Slashdot.)



(Post a new comment)

What's new?
[info]hogwood
2005-05-30 11:37 am UTC (link)
I've been using similar functions for a while:
Array.prototype.each = function each(block, that) {
	for (var i = 0; i < this.length; ++i) {
		block.call(that, this[i], i)
	}
}

Array.prototype.collect = function collect(block, toArray, that) {
	if (!toArray) toArray = []
	this.each(function() {
		toArray.push(block.apply(that, arguments))
	})
	return toArray
}

Of course, my JS becomes more and more Ruby-like over time. For example, my actual implementation uses a favorite helper function:
modClass(Array,
	function each(block, that) {
		...
	},
	function collect(block, toArray, that) {
		...
	}
)

Now, if the Mozilla guys would add something like 'method_missing', we'd be in for good times.

(Reply to this)(Thread)

Re: What's new?
[info]premshree
2005-05-30 11:57 am UTC (link)
I know that it definitely could’ve been done long ago—on most Good Browsers—but what’s new is that it’d be part of Mozilla’s JavaScript specs.

You and me using it rather hackishly is different from tons of people using it Mozillaishly. :-)

Something like method_missing would be uber-cool, yes.

(Reply to this)(Parent)


(Anonymous)
2005-05-30 06:14 pm UTC (link)
I've done more of the same at https://flgr.0x42.net/ruby.js/ a while ago. It reimplements quite a lot of Ruby's standard library in JavaScript.

-- flgr

(Reply to this)(Thread)


[info]premshree
2005-05-31 04:03 am UTC (link)
Heh, nice. Good stuff.

(Reply to this)(Parent)

ruby.js
(Anonymous)
2005-06-01 07:59 pm UTC (link)
Florian: I don't know if you're still actively maintaining that library, but in case you are, I noticed an error in your implementation of Array#at (Array.prototype.at).

The length is calculated (if index isA Range) after resolving 'end' to a positive value but before resolving 'start' to a positive value. So if the index is a range and the start value is negative, the length used in the slice is off.

I'll be reading through the rest of ruby.js and would like to submit patches for errors like this one that I've found. If you or someone else is maintaining this file as a project, can you direct me to where to submit patches. If it's not maintained, I'd like permission to take it under my wing, so to speak.

Premshee: forgive me for usurping your blog comments for a bug report in someone else's project. :)

Jacob Fugal

(Reply to this)(Parent)(Thread)

Re: ruby.js
[info]premshree
2005-06-01 09:02 pm UTC (link)
Dunno if he’ll see your comment. You might wanna get in touch with him via the mailing list.

(Reply to this)(Parent)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…