views:

57

answers:

4

Are there any frameworks/wrapper out there that gives us rubyish javascript?

Instead of the usual for() {} loop gives us the object.each {} loop like in Ruby?

Since javascript could be used in web browsers I do want to use it for the server side too, but I do like ruby syntax far more.

+2  A: 

look up jQuery. it has a

$('.css-selector').each(function(i){
 //do stuff
});

Ref: http://api.jquery.com/jQuery.each/

Moin Zaman
A: 

Here's a post by Ken Egozi which discusses adding .forEach and other helpers to the array prototype.

Larry K
+2  A: 

You might want to checkout JS.Class - Ruby-style JavaScript. From the docs,

JS.Class is a set of tools designed to make it easy to build robust object-oriented programs in JavaScript. It’s based on Ruby, and gives you access to Ruby’s object, module and class systems, some of its reflection and metaprogramming facilities, and a few of the packages from its standard library. It also provides a powerful package manager to help load your applications as efficiently as possible.

It comes with a well packaged standard library including modules and classes such as

  • Enumerable
  • Hash
  • Set
  • Observable
  • Command

The Enumerable module, for instance, is comparable to that in Ruby, and includes methods like

all
any
collect
drop
findAll
forEach
grep
partition
reject
select
zip
Anurag
+2  A: 

The Prototype library, having been developed by guys very close to Ruby on Rails, has a very Ruby-ish feel. It uses Ruby lingo (like mixins); for instance, the Enumerable mixin (which Prototype mixes in to arrays by default) adds the each method to an array, so you can do this:

["sample", "array"].each(function (item) {
    console.log(item);
});
Anthony Mills