Opal to convert Ruby code into JavaScript

Source: Internet
Author: User

Point here

Https://github.com/opal

Opal

Opal is a ruby to JavaScript Source-to-source compiler. It also have an implementation of the Ruby Corelib.

Opal is hosted on GitHub, and there are a freenode IRC channel at #opal, ask questions Onstackoverflow (tag #opalrb).

Usage

See the website, http://opalrb.org.

compiling Ruby code

Opal.compileis a simple interface to just compile a string of Ruby into a string of JavaScript code.

Opal.compile ("puts ' wow '")  # = "(function () {... self $puts (" wow "); ... }) ()"

Running this by itself are not enough and you need the Opal runtime/corelib.

Building the Corelib

Opal::BuilderCan is used to build the runtime/corelib into a string.

Opal::builder.build (' Opal#=> ' (function () {...}) ()"
Running compiled code

You can write the above of the strings to file, and run as:

<! DOCTYPE html><html>  <head>    <src="Opal.js" ></  Script> <src="App.js" ></script> </head></html> 

Just open a browser to this page and view the console.

Running tests

First, install dependencies:

$ bundle install

Rubyspec related repos must be cloned as a gitsubmodules:

$ git submodule update --init

The test suite can be run using (requires PHANTOMJS):

$ rake

This would command would run all RSpec and mspec examples in sequence.

Automated runs

A with Guardfile decent mappings between specs and lib/corelib/stdlib files are in place. Run to has bundle exec guard -i it started.

Mspec

Mspec tests can is run with:

$ rake mspec

Alternatively, you can just load up a rack instance using rackup , and visit in any http://localhost:9292/ Web browser.

Rspec

RSpec tests can run with

$ rake rspec
Code Overview

What's supposed to run where?

    • lib/Code runs inside your ruby env. It compiles ruby to JavaScript.
    • opal/is the runtime/corelib for our implementation (runs in browser)
    • stdlib/Is our implementation of Ruby Stdlib. It is optional (for browser).
Lib

The lib directory holds the Opal Parser/compiler used to compile Ruby into JavaScript. It is also built ready for the browser into to allow compilation in any opal-parser.js JavaScript environment.

corelib

This directory holds the Opal runtime and Corelib implemented in Ruby and JavaScript.

Stdlib

Holds the stdlib that Opal currently supports. This includes,, Observable StringScanner Date , etc.

Browser Support
    • Internet Explorer 6+
    • Firefox (Current-1) or current
    • Chrome (Current-1) or current
    • Safari 5.1+
    • Opera 12.1x or (Current-1) or current

Any problems encountered using the browsers listed above should is reported as a bug.

(Current-1) or current denotes this we support the current stable version of the browser and the version that preceded I T. For example, if the current version of a browser are 24.x, we support the 24.x and 23.x versions.

12.1x or (Current-1) or current denotes, we support opera 12.1x as well as the last 2 versions of opera. For example, if the current opera version was 20.x, then we support Opera 12.1x, 19.x and 20.x but not Opera 15.x through 1 8.x.

Cross-browser testing is sponsored by Browserstack.

License

(The MIT License)

Copyright (C) by Adam Beynon

Permission is hereby granted, free of charge, to all person obtaining a copy of this software and associated documentation Files (the "Software"), to deal in the software without restriction, including without limitation the rights to use, copy , modify, merge, publish, distribute, sublicense, and/or sell copies of the software, and to permit persons to whom Ftware is furnished to does so, subject to the following conditions:

The above copyright notice and this permission notice shall being included in all copies or substantial portions of the SOFTW Is.

The software is provided ' as is ', without WARRANTY of any KIND, EXPRESS OR implied, including and not LIMITED to the Warra Nties of merchantability, FITNESS for A particular PURPOSE and noninfringement. In NO EVENT shall the AUTHORS or COPYRIGHT holders is liable for any CLAIM, damages OR other liability, WHETHER in an ACTI On contract, TORT or OTHERWISE, arising from, out of or in CONNECTION with the software or the use or other dealings in The software.

Opal to convert Ruby code into JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.