-
-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1113 from opal/elia/opal-parser-docs
Add documentation for opal-parser (including Opal.eval)
- v1.8.3.rc1
- v1.8.2
- v1.8.1
- v1.8.0
- v1.8.0.beta1
- v1.8.0.alpha1
- v1.7.4
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- v1.7.0.rc1
- v1.6.1
- v1.6.0
- v1.6.0.rc1
- v1.6.0.alpha1
- v1.5.1
- v1.5.0
- v1.5.0.rc1
- v1.4.1
- v1.4.0
- v1.4.0.alpha1
- v1.3.2
- v1.3.1
- v1.3.0
- v1.3.0.rc1
- v1.3.0.alpha1
- v1.2.0
- v1.2.0.beta1
- v1.1.1
- v1.1.1.rc1
- v1.1.0
- v1.1.0.rc1
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v1.0.0.beta1
- v0.11.4
- v0.11.3
- v0.11.2
- v0.11.1
- v0.11.1.pre
- v0.11.0
- v0.11.0.rc1
- v0.10.6
- v0.10.6.beta
- v0.10.5
- v0.10.4
- v0.10.3
- v0.10.2
- v0.10.1
- v0.10.0
- v0.10.0.rc2
- v0.10.0.rc1
- v0.10.0.beta5
- v0.10.0.beta4
- v0.10.0.beta3
- v0.10.0.beta2
- v0.10.0.beta1
- v0.9.4
- v0.9.3
- v0.9.2
- v0.9.1
- v0.9.0
- v0.9.0.rc1
- v0.9.0.beta2
- v0.9.0.beta1
- v0.0.1-alpha.1
- npm-0.0.1-alpha.8
- npm-0.0.1-alpha.7
- npm-0.0.1-alpha.6
- npm-0.0.1-alpha.5
- npm-0.0.1-alpha.4
- npm-0.0.1-alpha.3
- lerna-0.0.1-alpha.2
Showing
4 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Parsing Ruby from JavaScript with `opal-parser` | ||
|
||
Generally the is best to percompile Ruby source files to JavaScript serverside but sometimes may become useful to be able to compile Ruby to JavaScript directly from JS. | ||
|
||
Opal is able to compile its – pure Ruby – compiler to JavaScript (how cool is that!). The whole compiler chain is available in the `stdlib` as `opal-parser`. | ||
|
||
```ruby | ||
require 'opal-parser' | ||
``` | ||
|
||
|
||
## Features | ||
|
||
### `Kernel#eval` | ||
|
||
`opal-parser` provides a partial implementation of `Kernel#eval` that only support the first parameter (String) and refuse any passed `binding`, `lineno` or `filename`. | ||
|
||
Example: | ||
|
||
```ruby | ||
require 'opal-parser' | ||
eval "puts 'hello world!'" | ||
``` | ||
|
||
|
||
### `Kernel#require_remote` | ||
|
||
Will fetch a remote URL (by means of a sync `XMLHttpRequest`) and evaluate its contents as Ruby code. | ||
|
||
Example: | ||
|
||
```ruby | ||
require 'opal-parser' | ||
require_remote 'http://pastie.org/pastes/10444960/text' | ||
HelloWorld.new.say_hello! | ||
``` | ||
|
||
|
||
### `Opal.compile()` and `Opal.eval()` (JavaScript) | ||
|
||
After requiring `opal-parser` both `Opal.compile()` and `Opal.eval()` functions are added to the JavaScript API. | ||
|
||
`Opal.compile(string, options)` (JavaScript) will forward the call to `Opal.compile` (Ruby) converting options from a plain JS object to a Ruby Hash. | ||
`Opal.eval(string)` will compile the given code to JavaScript and then pass it to the [native `eval()` function][eval]. | ||
|
||
|
||
### Support for `<script type="text/ruby">` | ||
|
||
When `opal-parser` is required it will search the page for any `<script>` tag with type `text/ruby`. | ||
If an `src` attribute is present will fetch and eval the file with `Kernel#require_remote` otherwise it will get the script tags contents and eval them with `Kernel#eval`. | ||
|
||
|
||
[eval]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters