-
Notifications
You must be signed in to change notification settings - Fork 220
Deal with method_missing #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There is no good support for 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 |
I can, but that doesn't look very DRY. Looking at my example, if An alternative could be for
|
Have just run into this same issue but mine is a bit more complicated. I'm making a game and have a subclass system like this: module Entities
class Base
attr_reader :model
def respond_to_missing?(name)
@model&.respond_to?(name)
end
def method_missing(...)
@model&.send(...)
end
end
class Player < Base
def initialize
@model = ::Models::Player.new
end
end
class Chest < Base
def initialize
@model = ::Models::Inventory.new
end
end
end
module Models
class Base
end
class Player < Base
attr_reader :x, :y, :inventory
def initialize
@x = 123
@y = 321
@inventory = ::Models::Inventory.new
end
end
class Inventory < Base
attr_reader :items
def initialize
@items = [...]
end
end
end
player = ::Entities::Player.new
player.x
player.y
chest = ::Entities::Chest.new
chest.items The idea is that you deal with Entity classes, backed by models. The problem is that the method_missing part can't be typed with RBS, so The way around it that I'm currently doing is to add It would be nice, like @HoneyryderChuck mentioned, to be able to include a classes methods into another class in RBS. e.g. module Entities
class Player < Base
include ::Models::Player.instance_methods
end
end Would be great to have something official for this. |
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:The text was updated successfully, but these errors were encountered: