Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
Sorbete de longitud de matriz variable

Estoy empezando a jugar con la gema de sorbete. Tengo un método que espera y devuelve una matriz de objetos. La cuestión es que la longitud de la matriz es variada. ¿Cómo puedo escribir comprobar el método? Sigo obteniendo el Expected type [Object], got array of size 2

Aquí está mi método

 sig { params(foo: Array[Object]).returns(Array[Object]) } def bar(foo) # code to modify some of the attributes end
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

tl; dr Tiene un error de sintaxis. Utilice T::Array[Object] (no Array[Object] ).

Está utilizando una sintaxis de tipo incorrecta para matrices:

 # typed: strict extend T::Sig sig { params(foo: Array[Object]).returns(Array[Object]) } def bar(foo) # code to modify some of the attributes end

→ Ver en sorbet.run

Los errores muestran:

 editor.rb:5: Use T::Array[...], not Array[...] to declare a typed Array https://srb.help/5026 5 |sig { params(foo: Array[Object]).returns(Array[Object]) } ^^^^^^^^^^^^^ Note: Array[...] will raise at runtime because this generic was defined in the standard library Autocorrect: Use `-a` to autocorrect editor.rb:5: Replace with T::Array 5 |sig { params(foo: Array[Object]).returns(Array[Object]) } ^^^^^ editor.rb:5: Use T::Array[...], not Array[...] to declare a typed Array https://srb.help/5026 5 |sig { params(foo: Array[Object]).returns(Array[Object]) } ^^^^^^^^^^^^^ Note: Array[...] will raise at runtime because this generic was defined in the standard library Autocorrect: Use `-a` to autocorrect editor.rb:5: Replace with T::Array 5 |sig { params(foo: Array[Object]).returns(Array[Object]) } ^^^^^ Errors: 2

¿Por qué es como este?

El método [] en Array tiene un significado especial, pero Sorbet usa [] para argumentos de tipo genérico. Para sortear eso, Sorbet usa el espacio de nombres T:: para ciertas clases genéricas en la biblioteca estándar:

→ https://sorbet.org/docs/stdlib-generics

Lo que sucede en tu caso es que este código:

 Array[Object]

es equivalente a haber escrito esto:

 [Object]

(es decir, "hacer una matriz de longitud uno que contenga el valor Object "). [Object] en Sorbet resulta ser la forma de expresar una 1-tuple .

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!