module RubyPython::Operators

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.

Public Class Methods

bin_op(rname, pname) click to toggle source

Creates a method to delegate a binary operation.

rname

The name of the Ruby method for this operation. Can be either a

Symbol or a String.

pname

The name of the Python magic method to which this method should

be delegated.

# File lib/rubypython/operators.rb, line 17
def self.bin_op(rname, pname)
  define_method rname.to_sym do |other|
    self.__send__(pname, other)
  end
end
operator_() click to toggle source

Provides access to the Python operator module.

# File lib/rubypython/operators.rb, line 8
def self.operator_
  @@operator ||= RubyPython.import('operator')
end
rel_op(rname, pname) click to toggle source

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.

rname

The name of the Ruby method for this operation. Can be a Symbol

or a String.

pname

The name of the Python magic method to which this method should

be delegated.

# File lib/rubypython/operators.rb, line 32
def self.rel_op(rname, pname)
  define_method rname.to_sym do |other|
    RubyPython::Operators.operator_.__send__(pname, self, other).rubify
  end
end
unary_op(rname, pname) click to toggle source

Creates a method to delegate a relational operator. These methods are implemented with calls the operator module.

rname

The name of the Ruby method for this operation. Can be a Symbol

or a String.

pname

The name of the Python magic method to which this method should

be delegated.

# File lib/rubypython/operators.rb, line 44
def self.unary_op(rname, pname)
  define_method rname.to_sym do
    RubyPython::Operators.operator_.__send__(pname, self)
  end
end

Private Class Methods

python_interpreter_update(status) click to toggle source

Called by RubyPython when the interpreter is started or stopped so that the necessary preparation or cleanup can be done. For internal use only.

# File lib/rubypython/operators.rb, line 109
def python_interpreter_update(status)
  case status
  when :stop
    @@operator = nil
  end
end

Public Instance Methods

<=>(other) click to toggle source

Delegates Comparison to Python.

# File lib/rubypython/operators.rb, line 101
def <=>(other)
  RubyPython::PyMain.cmp(self, other)
end
[](index) click to toggle source

Delegates object indexed access to the wrapped Python object.

# File lib/rubypython/operators.rb, line 86
def [](index)
  self.__getitem__ index
end
[]=(index, value) click to toggle source

Delegates setting of various indices to the wrapped Python object.

# File lib/rubypython/operators.rb, line 91
def []=(index, value)
  self.__setitem__ index, value
end
include?(item) click to toggle source

Delegates membership testing to Python.

# File lib/rubypython/operators.rb, line 96
def include?(item)
  self.__contains__(item).rubify
end