Class: RubyPython::PyMainClass

Inherits:
BlankObject
  • Object
show all
Includes:
Singleton
Defined in:
lib/rubypython/pymainclass.rb

Overview

A singleton object providing access to the python __main__ and __builtin__ modules. This can be conveniently accessed through the already instaniated PyMain constant. The __main__ namespace is searched before the __builtin__ namespace. As such, naming clashes will be resolved in that order.

## Block Syntax The PyMainClass object provides somewhat experimental block support. A block may be passed to a method call and the object returned by the function call will be passed as an argument to the block.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BlankObject

hide

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegates any method calls on this object to the Python __main__ or __builtin__ namespaces. Method call resolution occurs in that order.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubypython/pymainclass.rb', line 33

def method_missing(name,*args,&block)
  proxy = if main.respond_to?(name)
            main
          elsif builtin.respond_to?(name)
            builtin
          else
            super(name, *args)
          end
  result = if proxy.is_real_method?(name)
             proxy.__send__(name, *args)
           else
             proxy.__send__(:method_missing, name,*args)
           end
  block ? block.call(result) : result
end

Instance Attribute Details

#builtinRubyPyModule

namespace.

Returns:

  • (RubyPyModule)

    a proxy object wrapping the Python __builtin__



27
28
29
# File 'lib/rubypython/pymainclass.rb', line 27

def builtin
  @builtin||=RubyPython.import "__builtin__"
end

#mainRubyPyModule

namespace.

Returns:

  • (RubyPyModule)

    a proxy object wrapping the Python __main__



21
22
23
# File 'lib/rubypython/pymainclass.rb', line 21

def main 
  @main||=RubyPython.import "__main__"
end