mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
README.rdoc
This commit is contained in:
parent
88707e09e2
commit
aa0a2a999b
1 changed files with 27 additions and 5 deletions
30
README.rdoc
30
README.rdoc
|
@ -17,11 +17,10 @@ Embed the Mozilla Rhino Javascript interpreter into Ruby
|
|||
1. Ruby Objects goes into Javascript
|
||||
1. Our shark's in the Javascript!
|
||||
|
||||
# evaluate some simple javascript
|
||||
require 'rhino'
|
||||
|
||||
Rhino::Context.open do |context|
|
||||
context.eval("7 * 6") #=> 42
|
||||
end
|
||||
# evaluate some simple javascript
|
||||
eval_js "7 * 6" #=> 42
|
||||
|
||||
# evaluate a ruby function from javascript
|
||||
|
||||
|
@ -30,6 +29,29 @@ Embed the Mozilla Rhino Javascript interpreter into Ruby
|
|||
context.eval("say("Hello", 3)") #=> HelloHelloHello
|
||||
end
|
||||
|
||||
# embed a ruby object into your javascript environment
|
||||
|
||||
class MyMath
|
||||
def plus(lhs, rhs)
|
||||
lhs + rhs
|
||||
end
|
||||
end
|
||||
|
||||
Rhino::Context.open do |context|
|
||||
context["math"] = MyMath.new
|
||||
context.eval("math.plus(20,22)") #=> 42
|
||||
end
|
||||
|
||||
# make a ruby object *be* your javascript environment
|
||||
math = MyMath.new
|
||||
Rhino::Context.open(:with => math) do |context|
|
||||
context.eval("plus(20,22)") #=> 42
|
||||
end
|
||||
|
||||
#or the equivalent
|
||||
|
||||
math.eval_js("plus(20,22)")
|
||||
|
||||
# Configure your embedding setup
|
||||
|
||||
# Make your standard objects (Object, String, etc...) immutable
|
||||
|
|
Loading…
Reference in a new issue