I have this preudo code that describes a type
type MyType1 = {
type: :type1,
field1: number,
field2: any
} | {
type: :type2,
field3: string
} | {
type: :type4,
field4: SomeOtherType
} | {
type: :type5,
field5: string,
field6: integer,
field7: float
} | {
type: :type6
}
I've expressed it this way in Ruby:
class MyType1
attr_reader :type, :field1, :field2, :field3, :field4, :field5, :field6, :field7
def init_with_type_1(field1:, field2:)
@type = :type1
@field1 = field1
@field2 = field2
end
def init_with_type_2(field3:)
@type = :type2
@field3 = field3
end
# and so on...
end
Is there a better, more idiomatic, more simple way?
I don't consider using third-party gems and libraries.