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
32
README.rdoc
32
README.rdoc
|
@ -17,18 +17,40 @@ Embed the Mozilla Rhino Javascript interpreter into Ruby
|
||||||
1. Ruby Objects goes into Javascript
|
1. Ruby Objects goes into Javascript
|
||||||
1. Our shark's in the Javascript!
|
1. Our shark's in the Javascript!
|
||||||
|
|
||||||
|
require 'rhino'
|
||||||
|
|
||||||
# evaluate some simple javascript
|
# evaluate some simple javascript
|
||||||
|
eval_js "7 * 6" #=> 42
|
||||||
Rhino::Context.open do |context|
|
|
||||||
context.eval("7 * 6") #=> 42
|
|
||||||
end
|
|
||||||
|
|
||||||
# evaluate a ruby function from javascript
|
# evaluate a ruby function from javascript
|
||||||
|
|
||||||
Rhino::Context.open do |context|
|
Rhino::Context.open do |context|
|
||||||
context["say"] = lambda {|word, times| word * times}
|
context["say"] = lambda {|word, times| word * times}
|
||||||
context.eval("say("Hello", 3)") #=> HelloHelloHello
|
context.eval("say("Hello", 3)") #=> HelloHelloHello
|
||||||
end
|
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
|
# Configure your embedding setup
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue