Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Saturday, July 10, 2010

JavaScript tricks in Ruby

I attended a JavaScript workshop in Vancouver today and the talk by John Resig was inspiring. I pick up some notices here.

Splittinng arguments

Math.min() accepts a set of values. When you would like to find the minimum value of an array, you can use the following trick:

Math.min.apply(Math, the_array);

Function#apply receives an object for "text" and arguments in array.

On the other hand Ruby has a feature of this issue as a syntactic sugar.


Abbreviating "new"

JavaScript uses "new" operator to create an instance.

var a = new A(x);

You may want to abbreviate the keyword "new" like

var a = A(x);

You can provide such class with the following trick.