Javascript.new

Object.prototype[‘new’]=function(){
var params=[];
var argc=arguments.length;
for(var i=0;i<argc;i++){
params.push(‘arguments[’+i+’]’);
}
var code=‘return new this(’+params.join(’,’)+’);’;
return new Function(code).apply(this,arguments);
}

Number.new(100);//100
String.new(“text”);//“text”
Array.new(1,2,3);//[1,2,3]

function Person(name){
this.name=name;
this.toString=function(){return this.name;}
}
tom=Person.new(‘Tom’);
joy=Person.new(‘Joy’);
tom+" and "+joy;

There seems to be a javascript toolkit with ruby-style syntax …

Out of the sandbox ?

Few people know that on windows, if you rename .html to .hta, then
javascript will gain the ability of accessing filesystem, spawning
process, even calling DirectX. So you can write fully-featured gui
programs in javascript with notepad, without install anything else …

Another popular javascript tool is Rhino, which is cross-platform.

Ruby syntax is much nicer than javascript syntax.

Didn’t you know god kills a kitten whenever one does:

function SomeClass(name){

?

Javascript is indeed a fine language - but as you’re probably aware,
this is the ruby-talk mailing list. Is your point that you can do
Ruby-like things in Javascript? Sure can.

Number.prototype.times = function(thing) {
for (var i=0; i<this; i++) thing();
}

Number(5).times(function() { print(“hello”) })

Javascript might be a great general-purpose language if it weren’t so
sandboxed. That is, whilst you can get a Javascript interpreter which
runs on the command line (spidermonkey), it can’t talk to the filesystem
or spawn external processes.

When you are developing a gem, and you want to add examples, how do
you get your example to find your gem without installing your gem?

i.e. suppose you have

mygem/
examples/
example1.rb
lib/

and example1.rb starts with
require “mygem”

but your gem isn’t installed yet so how does example1.rb find the
right code?

Is there some trick for this, or do people just install their gem first?

On Thu, Jun 25, 2009 at 12:12 PM, Martin H.[email protected] wrote:

and example1.rb starts with
require “mygem”

but your gem isn’t installed yet so how does example1.rb find the right
code?

Is there some trick for this, or do people just install their gem first?

You can do this to run your example:

mygem% ruby -I lib examples/example1.rb

The ‘-I lib’ will add ‘lib’ to the include path for Ruby.

On Jun 25, 2009, at 11:12 , Martin H. wrote:

When you are developing a gem, and you want to add examples, how do
you get your example to find your gem without installing your gem?

PLEASE do not thread hijack. It isn’t hard to start a new email fresh
and use your address book.

personally,
I munge the load path in my example:

$: << “…/lib” # or whatever that you need

/Shawn