I have a module which exists to be included in two similar classes. Some of the methods to be included in the module for identical use by both classes return a new instance.
But how to I encode in the module that the constructor for the containing class should be called?
A simplified example:
module Point3D
def initialize(x,y,z)
@x = x
@y = y
@z = z
end
def * (scalar)
<myclass>.new(@x * scalar, @y * scalar, @z * scalar)
end
end
class Vertex
include Point3D
end
class Vector
include Point3D
end
So in the definition of * how would i call the constructor such that in the context of the Vertex class it returned a new Vertex and in the context of the Vector class it returned a new Vector without redeclaring all such methods in each class?
You can call ‘class’ method to get the class of obj.
For this case, it’s