点这里

https://github.com/opal

Opal

  

Opal is a ruby to javascript source-to-source compiler. It also has an implementation of the ruby corelib.

Opal is hosted on github, and there is a Freenode IRC channel at #opal, ask questions onstackoverflow (tag #opalrb).

  

Usage

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

Compiling ruby code

Opal.compile is 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 is not enough, you need the opal runtime/corelib.

Building the corelib

Opal::Builder can be used to build the runtime/corelib into a string.

Opal::Builder.build('opal') #=> "(function() { ... })()"

Running compiled code

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

<!DOCTYPE html>
<html>
<head>
<script src="opal.js"></script>
<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 will command will run all RSpec and MSpec examples in sequence.

Automated runs

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

MSpec

MSpec tests can be run with:

$ rake mspec

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

Rspec

RSpec tests can be run with

$ rake rspec

Code Overview

What code is 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 opal-parser.js to allow compilation in any 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 ObservableStringScannerDate, 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 be reported as a bug.

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

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

Cross-browser testing is sponsored by BrowserStack.

License

(The MIT License)

Copyright (C) 2013 by Adam Beynon

Permission is hereby granted, free of charge, to any 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 the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

opal 将ruby代码转化成javascript的更多相关文章

  1. [转]DSL-让你的 Ruby 代码更优秀

    https://ruby-china.org/topics/38428 以下摘录 DSL和Gpl DSL : domain-specific language.比如HTML是用于组织网页的‘语言’, ...

  2. 50行ruby代码开发一个区块链

    区块链是什么?作为一个Ruby开发者,理解区块链的最好办法,就是亲自动手实现一个.只需要50行Ruby代码你就能彻底理解区块链的核心原理! 区块链 = 区块组成的链表? blockchain.ruby ...

  3. MyEclipse格式化JSP代码,其中Javascript无法格式化的原因

    MyEclipse格式化JSP代码,其中Javascript无法格式化的原因: 可能是JSP页面代码有错误的地方,而且可能是一个很微小的错误,比如多写了一个标点符号,这个需要仔细检查,包括HTML.C ...

  4. Windows环境Vim编辑器如何执行Ruby代码

    1.下载 Ruby 1.8.5(2006-8-25) for Windows: 在网页http://www.rubychina.net/downloads/ 上找到  --〉Ruby on Windo ...

  5. 在Notepad++下运行ruby代码

    轻量级,轻量级,所以用notepad++来运行ruby的代码最合适不过了,虽说有更好用的轻量级工具,但是用notepad++习惯了,也懒得去再装其他工具了.好了,进入主题,先安装插件NppExec,打 ...

  6. 两行代码搞定 JavaScript 的日期验证

    我们通常在 JavaScript 中验证日期,基本的思路大概是,先判断年月日是否有效,再判断当月是否有当日,比如一些月份没有 31 日,平年二月没有 29.30 日,闰年二月没有 30 日等等. 偶然 ...

  7. 高质量的javascript代码 -- 深入理解Javascript

    一. 编写高质量的javascript代码基本要点a) 可维护的代码(Writing Maintainable Code)i. 可读(注释)ii. 一致(看上去是同一个人写的)iii. 已记录b) 最 ...

  8. 读《编写高质量代码:改善JavaScript程序的188个建议》1

    建议3:减少全局变量污染 定义全局变量有3种方式: ❑在任何函数外面直接执行var语句. var f='value'; ❑直接添加一个属性到全局对象上.全局对象是所有全局变量的容器.在Web浏览器中, ...

  9. [置顶] Ruby,Scala和JavaScript中的函数式编程(一)

    函数式编程(英语:Functional programming)或者函数程序设计,又称泛函编程,是一种编程范型,它将电脑运算视为数学上的函数计算,并且避免使用程序状态以及易变对象.函数编程语言最重要的 ...

随机推荐

  1. char const*, char*const, const char *const的区别

    C++标准规定,const关键字放在类型或变量名之前等价的.所以,const char*和 char const*是一样的. const char*   //常量指针---指向常量的指针----指针指 ...

  2. .NET开源工作流RoadFlow-流程设计-流程属性设置

    打开流程设计器点击新建流程后会弹出新流程属性设置框,属性设置包括基本信息,数据连接和标识字段三项设置. 1.基本信息 流程ID:系统自动给每个流程分配的一个唯一ID. 流程名称:给您的新流程取一个名称 ...

  3. ORA-00265: instance recovery required, cannot set ARCHIVELOG

    OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...

  4. arcgis中使用excel中x,y坐标创建点问题

    文件——从x,y中添加,可以显示点的位置 右击图层导出数据时,出现无法绘制图形,生成shapefile文件的情况.经过排除数据发现 当x,y坐标值中出现null等异常值时,会出现上述无法导出的情况.

  5. Easy-UI 动态添加DataGrid的Toolbar按钮

    在前人的基础上进行的修改,不知道他是从哪里引用来的,所以没有粘贴引用地址. 原代码不支持1.3.6. 修改功能: 1.如果之前没有添加过工具,用这个方法不能添加(已修复): 2.估计是不支持1.3.6 ...

  6. 有关ZxMiddleTier构想

    终于决定动手写一个自己的东西,没有其它想法,人已经30多了,感觉原来学的东西都是零散的,想通过这样一个中间层的项目,串联起原来所学的东西,形成一个体系,也算是对自己这么多年的程序员的生涯做出一个交代, ...

  7. SQLite数据库的基本API函数

    1 .打开数据库: 说明:打开一个数据库,文件名不一定要存在,如果此文件不存在, sqlite 会自动创建.第一个参数指文件名,第二个参数则是定义的 sqlite3 ** 结构体指针(关键数据结构), ...

  8. expect脚本

    #!/bin/bash # Filename: main.sh usage(){ echo "scp file :./main.sh scp sourcefile destfile" ...

  9. Java下Web MVC的领跑者:SpringMVC

    比较常用的MVC框架有Struts 和 SpringMVC. Struts 是Java Web MVC框架中曾经不争的王者.经过长达九年的发展,Struts占有了MVC框架中最大的市场份额.但是Str ...

  10. jquery实现select下拉框可输入+联想关联option

    下面代码摘自http://www.oschina.net/question/96791_12832 <script language="javascript" src=&qu ...