Module: Rupy::Operators

Included in:
RubyPyProxy
Defined in:
lib/rupy/operators.rb

Overview

A mixin module to provide method delegation to a proxy class. This is done either by delegating to methods defined on the wrapped object or by using the Python operator module. A large number of the methods are dynamically generated and so their documentation is not provided here. In general all operators that can be overloaded are delegated.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bin_op(rname, pname) ⇒ Object

Creates a method to delegate a binary operation. The result of the operation will follow the conversion rules appropriate to the current mode of operation as set by RubyPython.legacy_mode. @param[Symbol, String] rname The name of the Ruby method for this operation @param pname The name of the Python magic method to which this method should be delegated.



20
21
22
23
24
# File 'lib/rupy/operators.rb', line 20

def self.bin_op rname, pname
    define_method rname.to_sym do |other|
        self.__send__ pname, other
    end
end

.operator_Object

Provides access to the Python operator module. @return



10
11
12
# File 'lib/rupy/operators.rb', line 10

def self.operator_
    @@operator ||= Rupy.import('operator')
end

.rel_op(rname, pname) ⇒ Object

Creates a method to delegate a relational operator. The result of the delegated method will always be converted to a Ruby type so that simple boolean testing may occur. These methods are implemented with calls the operator module. @param[Symbol, String] rname The name of the Ruby method for this operation @param pname The name of the Python magic method to which this method should be delegated.



33
34
35
36
37
# File 'lib/rupy/operators.rb', line 33

def self.rel_op rname, pname
    define_method rname.to_sym do |other|
        Operators.operator_.__send__(pname, self, other).rubify
    end
end

.unary_op(rname, pname) ⇒ Object

Creates a method to delegate a relational operator. The result of the operation will follow the conversion rules appropriate to the current mode of operation as set by RubyPython.legacy_mode. These methods are implemented with calls the operator module. @param[Symbol, String] rname The name of the Ruby method for this operation @param pname The name of the Python magic method to which this method should be delegated.



46
47
48
49
50
# File 'lib/rupy/operators.rb', line 46

def self.unary_op rname, pname
    define_method rname.to_sym do
        Operators.operator_.__send__(pname, self)
    end
end

Instance Method Details

#<=>(other) ⇒ Object

Delegates Comparison to Python.



106
107
108
# File 'lib/rupy/operators.rb', line 106

def <=>(other)
    PyMain.cmp(self, other)
end

#[](index) ⇒ Object

Delegates object indexed access to the wrapped Python object.



91
92
93
# File 'lib/rupy/operators.rb', line 91

def [](index)
    self.__getitem__ index
end

#[]=(index, value) ⇒ Object

Delegates setting of various indices to the wrapped Python object.



96
97
98
# File 'lib/rupy/operators.rb', line 96

def []=(index, value)
    self.__setitem__ index, value
end

#include?(item) ⇒ Boolean

Delegates membership testing to Python.

Returns:

  • (Boolean)


101
102
103
# File 'lib/rupy/operators.rb', line 101

def include?(item)
    self.__contains__(item).rubify
end