Method: RubyPython::RubyPyProxy#method_missing

Defined in:
lib/rubypython/rubypyproxy.rb

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

Delegates method calls to proxied Python objects.

Delegation Rules

  1. If the method ends with a question-mark (e.g., nil?), it can only be a Ruby method on RubyPyProxy. Attempt to reveal it (RubyPyProxy is a BlankObject) and call it.

  2. If the method ends with equals signs (e.g., value=) it’s a setter and we can always set an attribute on a Python object.

  3. If the method ends with an exclamation point (e.g., foo!) we are attempting to call a method with keyword arguments.

  4. The Python method or value will be called, if it’s callable.

  5. RubyPython will wrap the return value in a RubyPyProxy object

  6. If a block has been provided, the wrapped return value will be passed into the block.

Raises:

  • (NoMethodError)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rubypython/rubypyproxy.rb', line 135

def method_missing(name, *args, &block)
  name = name.to_s

  if name =~ /\?$/
    begin
      RubyPyProxy.reveal(name.to_sym)
      return self.__send__(name.to_sym, *args, &block)
    rescue RuntimeError => exc
      raise NoMethodError.new(name) if exc.message =~ /Don't know how to reveal/
      raise
    end
  end

  kwargs = false

  if name =~ /=$/
    return @pObject.setAttr(name.chomp('='),
                            PyObject.new(args.pop))
  elsif name =~ /!$/
    kwargs = true
    name.chomp! "!"
  end

  raise NoMethodError.new(name) if !@pObject.hasAttr(name)

  pFunc = @pObject.getAttr(name)

  if pFunc.callable?
    if args.empty? and pFunc.class?
      pReturn = pFunc
    else
      if kwargs and args.last.is_a?(Hash)
        pKeywords = PyObject.new args.pop
      end
      pReturn = _method_call(pFunc, args, pKeywords)
      pFunc.xDecref
    end
  else
    pReturn = pFunc
  end

  result = _wrap(pReturn)

  if block
    block.call(result)
  else
    result
  end
end