I am new to ruby and lots of things are confusing me. I believe this specific one is from Sorbet which is some type checking library? not sure. This specific piece of code is right before a method declaration
sig { params(order: T::Hash[
String, T.any(
T.nilable(String), T::Hash[
String, T.any(
Integer, T.nilable(String)
)
]
)
]).returns(Types::Order) }
def self.build_prescription(prescription)
# method implementation
The order
object is a json object coming from REST API. Can someone explain what this nesting thing is going on.
This is indeed a Sorbet signature. You can read more about Sorbet in their official documentation
The signature itself in this case is just describing the shape of the parameters and return type of the
build_prescription
method.Slightly reformatting to make it easier to annotate:
Note, though, that this will fail a Sorbet validation, as the signature declares the parameter as
order
, while the method declares it asprescription
. Those two names should match.There's a way to re-write this signature to make it a bit nicer to read/understand
Note that I'm moving the
T.nilable
s out one level, asT.any(Integer, T.nilable(String))
means that same asT.nilable(T.any(Integer, String))
but it's more immediately obvious for a reader that the value can benil