Created
January 17, 2011 19:36
-
-
Save cowboyd/783330 to your computer and use it in GitHub Desktop.
Proof of concept for making all ruby available to javascript.
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
require 'v8' | |
require 'openssl' | |
class Module | |
def [](name) | |
self.const_get(name) | |
end | |
end | |
class RubyTopLevel | |
def [](constant) | |
Object.const_get(constant) | |
end | |
end | |
V8::Context.new do |cxt| | |
cxt['Ruby'] = RubyTopLevel.new | |
cxt.eval('var cipher = new Ruby.OpenSSL.Cipher("aes-128-cbc")') | |
cxt.eval('cipher.decrypt') | |
puts cxt['cipher'] #=> #<OpenSSL::Cipher:0x1015c7f40> | |
end |
Hmm, true. Hadn't looked at the original code for the use of eval("::#{constant}")
, was just pointing out the two aren't the same.
General solution to this problem:
"Foo::Bar".split("::").inject(Object) { |m, e| m.const_get(e) }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@caius: in this case it will work because it will be invoked only with top level constants, hence
RubyTopLevel
.