Module: RubyPy

Defined in:
lib/ruby.py.rb

Overview

Ruby wrapper for Python modules.

Class Method Summary collapse

Class Method Details

.import(pym) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby.py.rb', line 9

def self.import(pym)
  raise ArgumentError unless pym =~ /\A[-_.A-Za-z0-9]+\Z/

  mod = pym.split('.').map { |s| s.split('_').map(&:capitalize).join }
  mod.size.times { |i| eval "module #{mod[0..i].join('::')}; end" }

  eval <<-EVAL
    module #{mod.join('::')}
      unless @pycall
        @pycall = Module.new do
          extend ::PyCall::Import
          pyimport #{pym.inspect}, as: 'import'
        end

        def self.method_missing(method_name, *arguments, &block)
          if respond_to_missing?(method_name)
            @pycall.import.send(method_name, *arguments, &block)
          else
            super
          end
        end
        def self.respond_to_missing?(method_name, include_private = false)
          @pycall.import.respond_to?(method_name, include_private)
        end
      end

      self
    end
  EVAL
end