rbs icon indicating copy to clipboard operation
rbs copied to clipboard

Deal with method_missing

Open HoneyryderChuck opened this issue 5 years ago • 3 comments

Will there be a way to deal with overriding method_missing? For instance, if I delegate the method calls to an instance variable, I'd like to "inherit" this object's API:


def method_missing(meth, *args)
  if @obj.respond_to?(meth)
    @obj.__send__(meth,  *args)
  else
    super
  end
end

HoneyryderChuck avatar Oct 16 '20 17:10 HoneyryderChuck

There is no good support for #method_missing. If you have a set of methods available in the class, you can write method type definitions for them even if it is implemented by #method_missing.

You can include an interface to declare a class has a set of methods.

interface _FooBar
  def foo: () -> void
  def bar: () -> void
end

class FooBarBaz
  include _FooBar           # Equivalent to writing defs for #foo and #bar.

  def baz: () -> void
end

soutaro avatar Oct 17 '20 07:10 soutaro

I can, but that doesn't look very DRY. Looking at my example, if @obj is an array for instance; Array definitions are done in a class, not an interface. So in order for it to work for me, I'd have to "repeat" the Array class definitions in interface.

An alternative could be for rbs to allow inclusion of classes, or even a DSL method to extract a class interface:

# something like:
classFooBarBaz
  include Array.interface

HoneyryderChuck avatar Oct 30 '20 09:10 HoneyryderChuck