opal 将ruby代码转化成javascript

点这里

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.

时间: 2024-08-26 02:47:13

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

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

https://ruby-china.org/topics/38428 以下摘录 DSL和Gpl DSL : domain-specific language.比如HTML是用于组织网页的‘语言’, CSS专门调整页面样式的‘语言’. SQL是数据库操作的‘语句’. GPL: general-purpose language.通用目的语言.即不是为了特定领域设计的语言.Ruby,Python,C都是. 简单的DSL 我们遇到不少的Ruby开源库都会有其对应DSL,其中就包括Rspec,Rabl,

你相信么,只需一个函数5行JS代码即可在Javascript中实现完整的AOP功能

你相信么,只需一个函数5行JS代码即可在Javascript中实现完整的AOP功能, 你相信么,在JavaScript只需一个函数5行代码即可实现完整的面向方面AOP编程功能.这5行代码的功能包括: 无限层次的函数无害拦截 函数执行前拦截 检查函数的参数值 重新设定函数的参数值 函数执行后拦截 检查函数执行后的返回结果 重新设定函数的返回结果 虽然动态函数式语言的效率是一个存在的问题,但是对于它的高度灵活性,简直让人令人惊叹不已,剧赞. 这个小小的函数源自于和爱明兄的一次讨论:在javascri

180行ruby代码搞定游戏2048

最今在玩2048这款小游戏,游戏逻辑简单,非常适合我这种对于游戏新入行的人来实现逻辑.于是选择了最拿手的ruby语言来实现这款小游戏的主要逻辑.还是挺简单的,加起来4小时左右搞定. 上代码: require 'optparse' module Help HELP_TEXT =<<HELP press buttons for move l => move to left r => move to right t => move to top b => move to bo

将代码转化成jQuery插件

将类似的jQuery代码,转化为插件,可以提高代码的重用性,并能够有效的组织代码. (function($){ $.fn.yourPluginName=function(){ //Your code goes here return this; } })(jQuery) 总结以便之后查阅. 将代码转化成jQuery插件

在Notepad++下运行ruby代码

轻量级,轻量级,所以用notepad++来运行ruby的代码最合适不过了,虽说有更好用的轻量级工具,但是用notepad++习惯了,也懒得去再装其他工具了.好了,进入主题,先安装插件NppExec,打开notepad++,在Plugin->Plugin Manager->Available里面找到NppExec,勾选中Install.装好重启后,在notepad++中打开一段ruby代码,F6在弹出的对话框输入 cd $(CURRENT_DIRECTORY) ruby $(FULL_CURRE

编写自己的代码库(javascript常用实例的实现与封装)

编写自己的代码库(javascript常用实例的实现与封装) 1.前言 大家在开发的时候应该知道,有很多常见的实例操作.比如数组去重,关键词高亮,打乱数组等.这些操作,代码一般不会很多,实现的逻辑也不会很难,下面的代码,我解释就不解释太多了,打上注释,相信大家就会懂了.但是,用的地方会比较,如果项目有哪个地方需要用,如果重复写的话,就是代码沉余,开发效率也不用,复用基本就是复制粘贴!这样是一个很不好的习惯,大家可以考虑一下把一些常见的操作封装成函数,调用的时候,直接调用就好!源码都放在githu

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

建议3:减少全局变量污染 定义全局变量有3种方式: ?在任何函数外面直接执行var语句. var f='value'; ?直接添加一个属性到全局对象上.全局对象是所有全局变量的容器.在Web浏览器中,全局对象名为window. window.f='value'; ?直接使用未经声明的变量,以这种方式定义的全局变量被称为隐式的全局变量. f='value'; 为方便初学者在使用前无须声明变量而有意设计了隐式的全局变量,然而不幸的是忘记声明变量成了一个非常普遍的现象.JavaScript的策略是让那

java util - 在java代码中执行javascript代码工具 rhino-1.7.7.jar

需要 rhino-1.7.7.jar 包 代码示例: package cn.java.mozilla.javascript; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; public class Test { public static void main(String[] args) { test4JsNormalCode(); test4JsObj(); test4JsFun

《编写高质量代码:改善JavaScript程序的188个建议》学习小记(一)

作者介绍: 成林,资深Web前端工程师,从事Web前端工作多年,精通CSS.HTML.JavaScript.jQuery和Ajax等Web前端技术,在实践中积累了大量的经验.推崇Web技术标准,曾经在多所高等院校和一些线下技术沙龙主讲Web标准和规范相关的课程,曾经还参与过W3C组织的标准化文档的中文编译工作.近几年来,集中精力研究和实践CSS3和 HTML 5前沿技术,在国内是该领域的先驱者之一. 其中书的目录分类为以下几部分: 第1章 JavaScript语言基础 第2章 字符串.正则表达式