Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Calling Ruby code from Python.

  • Loading branch information...
commit 1e107f8fe2306607b22e5b0b64dccd2a3422c665 1 parent 1a72147
@vic authored
View
6 examples/ruby.py
@@ -0,0 +1,6 @@
+from __ruby__ import require
+
+require('rbconfig')
+from __ruby__.Config.CONFIG import fetch as rb
+
+print rb("RUBY_INSTALL_NAME") # => rbx
View
23 lib/typhon/code_loader.rb
@@ -73,12 +73,35 @@ def self.load_module(name, from_module)
# Import +names+ from the +modname+ module into module +into+.
def self.import_from_module(modname, into, names)
names = Hash[*names] unless names.kind_of? Hash
+ return import_from_ruby(modname, into, names) if modname =~ /^__ruby__/
mod = load_module(modname, into)
names.each do |key, as|
into.py_set((as || key).to_sym, mod.py_get(key.to_sym))
end
end
+ def self.import_from_ruby(modname, into, names)
+ rb = Kernel
+ rb = modname.sub(/^__ruby__\.?/, '').split('.').inject(rb) do |o, n|
+ o.const_get(n)
+ end
+ names.each do |key, as|
+ value = nil
+ if key =~ /[A-Z]/ && rb.const_defined?(key)
+ value = rb.const_get(key)
+ elsif rb.respond_to?(key)
+ value = rb.method(key)
+ unless value.respond_to?(:invoke)
+ class << value; alias_method :invoke, :call end
+ end
+ else
+ raise "Dont know how to obtain #{key} from ruby object: #{rb.inspect}"
+ end
+
+ into.py_set((as || key).to_sym, value)
+ end
+ end
+
end
end
View
2  spec/examples/ruby.out
@@ -0,0 +1,2 @@
+#! typhon examples/ruby.py
+rbx
Please sign in to comment.
Something went wrong with that request. Please try again.