I have a mixin module where the default implementation of a certain method returns self, and classes that include the module that need to override the method are supposed to return an instance of their own type. Something like:
module Cleanable
# return [Cleanable] of the same type as this Cleanable, but clean
def cleaned
self
end
def empty?
false
end
# clean a list of [Cleanables]
def clean_all(cleanables)
cleanables.map(&:cleaned).reject(&:empty?)
end
end
class CleanableLeaf
include Cleanable
# uses default implementation
end
class CleanableBranch
include Cleanable
# return [CleanableBranch] a copy of this node with clean children
def cleaned
CleanableBranch.new(children: clean_all(self.children)
end
def empty?
children.empty?
end
end
A similar case would be something like Numeric#abs, which returns an Integer for Integers, a Float for Floats, a Rational for Rationals, etc.
Is there a way to document this in YARD, or is returning the mixin type the best I can do?
I have a mixin module where the default implementation of a certain method returns
self
, and classes that include the module that need to override the method are supposed to return an instance of their own type. Something like:A similar case would be something like
Numeric#abs
, which returns anInteger
forIntegers
, aFloat
forFloats
, aRational
forRationals
, etc.Is there a way to document this in YARD, or is returning the mixin type the best I can do?