# Javascript.new

**URL:** <https://www.ruby-forum.com/t/javascript-new/171026>\
**Category:** Ruby\
**Created:** [June 25, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/javascript-new/171026 "2009-06-25T13:38:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [June 25, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/javascript-new/171026/1 "2009-06-25T13:38:19Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [June 25, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/javascript-new/171026/2 "2009-06-25T19:56:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [June 25, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/javascript-new/171026/3 "2009-06-25T20:07:58Z")

</div>

Ruby syntax is much nicer than javascript syntax.

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

function SomeClass(name){

?

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [June 25, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/javascript-new/171026/4 "2009-06-25T14:33:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [June 25, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/javascript-new/171026/5 "2009-06-25T20:13:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [June 25, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/javascript-new/171026/6 "2009-06-25T20:16:32Z")

</div>

On Thu, Jun 25, 2009 at 12:12 PM, Martin H.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [June 25, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/javascript-new/171026/7 "2009-06-25T20:31:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [June 25, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/javascript-new/171026/8 "2009-06-25T20:19:27Z")

</div>

personally,  
I munge the load path in my example:

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

/Shawn
