简体   繁体   中英

Making new app with ringojs

Could you recommend any type pf resources around ringojs (tutorials, free ebooks, paid ebooks, etc.). The official website and the github documentation is only for set up purposes, so please do not paste ringojs.org and github links. Thanx in advance

Here's a short intro I wrote for a high-school beginning programming class. It's not extensive, but it's enough to give someone a basic start. The documentation (see oberhamsi's link) is great for experienced developers, but novice programmers sometimes need a little more explanation.

How do I write programs with RingoJS?

RingoJS uses modules to provide functionality like reading data from the keyboard, printing to the screen, reading and writing files, and even network operations. You can use a module in your program by calling the function require() with the module name. Here is a simple helloWorld.js example:

var system = require("system");
system.stdout.writeLine("hello, world");

That's pretty easy. Or, if you're going to be writing a lot of information to the screen, you could save yourself some typing by declaring a reference to stdout directly, like this:

var stdout = require("system").stdout;
stdout.writeLine("hello, world");

The second program does the same thing as the first one. There are benefits to each approach, so pick what works best for your program. Of course, you could customize the code even further like this:

var print = require("system").stdout.writeLine;
print("hello, world");

This is JavaScript. Let your imagination soar. You can do that.

How do I read data from the keyboard with RingoJS?

The system module provides an object called stdin , which allows your program to read keyboard input. You can use it together with the system's stdout object to write programs that do console I/O. Here is an example program called helloName.js that asks the user for their name and then says hello:

var main = function()
{
    "use strict";
    var stdout = require("system").stdout;
    var stdin = require("system").stdin;
    stdout.write( "What is your name? " );
    var name = stdin.readLine();
    stdout.writeLine( "Hello, " + name );
}();

What about numeric input?

The function stdin.readLine() returns a string. You can convert a string to an integer by calling the JavaScript function parseInt() . There is also a function called parseFloat() that converts a string into a floating-point numeric variable. Try this program called squared.js that uses parseInt() :

var main = function() {
    "use strict";
    var stdout = require("system").stdout;
    var stdin = require("system").stdin;
    stdout.write( "Enter an integer: " );
    var n = parseInt( stdin.readLine() );
    stdout.writeLine( "n squared is " + (n*n) );
}();

Can I read multiple input values from one line?

Absolutely! The JavaScript String class has a method called split() that will do the job. The following example program uses a JavaScript regular expression to split the string into an array of tokens. The benefit of using a regular expression is that it will work even if there are multiple spaces between the numbers. Regular expressions are an advanced topic, so don't panic if you don't understand how that part of the split command works. Just keep this example tucked away so you can use it when you want to read two or more pieces of information from a single line of input.

var main = function() {
    "use strict";
    var stdout = require("system").stdout;
    var stdin = require("system").stdin;
    stdout.write( "Enter rectangle height and width: " );
    var line = stdin.readLine();
    var tokens = line.split(/\s+/);
    var height = parseInt(tokens[0]);
    var width = parseInt(tokens[1]);
    stdout.writeLine( "The area of a " + height + " by " + width + " rectangle is " + (height*width) );
}();

有一个针对Web应用程序的教程: http : //ringojs.org/tutorial/ ,并且文档部分提供的内容比您想象的还要多http://ringojs.org/documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM