lib/rubypython/wrapper_extensions.rb in rubypython-0.2.2 vs lib/rubypython/wrapper_extensions.rb in rubypython-0.2.3
- old
+ new
@@ -1,10 +1,52 @@
+require 'singleton'
class RubyPythonBridge::RubyPyObject
def inspect
"<#{self.class}:#{__name}>"
end
end
+
+# 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 beofre 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.
+class PyMainClass
+ include Singleton
+ #:nodoc:
+ def main
+ @main||=RubyPython.import "__main__"
+ end
+
+ #:nodoc:
+ def builtin
+ @builtin||=RubyPython.import "__builtin__"
+ end
+
+ #:nodoc:
+ def method_missing(name,*args,&block)
+ begin
+ result=main.send(name,*args)
+ rescue NoMethodError
+ begin
+ result=builtin.send(name,*args)
+ rescue NoMethodError
+ super(name,*args)
+ end
+ end
+ if(block)
+ return block.call(result)
+ end
+ end
+end
+
+# See _PyMainClass_
+PyMain=PyMainClass.instance
# A wrapper class for Python Modules.
#
# Methods calls are delegated to the equivalent Python methods/functions. Attribute references
# return either the equivalent attribute converted to a native Ruby type, or wrapped reference
\ No newline at end of file