Javascript.new

H

Haoqi Haoqi

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;
 
B

Brian Candler

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.
 
L

Lui Core

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.
 
M

Marc Heiler

Ruby syntax is much nicer than javascript syntax.

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

function SomeClass(name){

?
 
M

Martin Hess

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?
 
L

Lee Hinman

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

i.e. suppose you have

mygem/
=A0 =A0 =A0 =A0examples/
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0example1.rb
=A0 =A0 =A0 =A0lib/

and example1.rb starts with
=A0 =A0 =A0 =A0require "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.
 
S

Shawn Anderson

[Note: parts of this message were removed to make it a legal post.]

personally,
I munge the load path in my example:

$: << "../lib" # or whatever that you need

/Shawn
 
R

Ryan Davis

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top