Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
¿Parámetros opcionales con bloques?
# multi_map # Write a method multi_map that accepts an array, an optional number (n), and a block as arguments. # The method should return a new array where each element of the original array is repeatedly run through the block n times. # If the number argument is not passed in, then the the elements should be run through the block once. # Examples def multi_map(arr, &blc, *n) narr = [] return narr end p multi_map(['pretty', 'cool', 'huh?']) { |s| s + '!'} # ["pretty!", "cool!", "huh?!"] p multi_map(['pretty', 'cool', 'huh?'], 1) { |s| s + '!'} # ["pretty!", "cool!", "huh?!"] p multi_map(['pretty', 'cool', 'huh?'], 3) { |s| s + '!'} # ["pretty!!!", "cool!!!", "huh?!!!"] p multi_map([4, 3, 2, 7], 1) { |num| num * 10 } # [40, 30, 20, 70] p multi_map([4, 3, 2, 7], 2) { |num| num * 10 } # [400, 300, 200, 700] p multi_map([4, 3, 2, 7], 4) { |num| num * 10 } # [40000, 30000, 20000, 70000]

Necesito hacer un método que tome una matriz, opcional y un bloque. Recibo un error con los argumentos que se llaman cuando ejecuto este programa básico en rspec. ¿Por qué es este el caso?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Considera lo siguiente:

 irb(main):010:1* def mm(arr, *n, &blc) irb(main):011:1* p arr; n.each(&blc) irb(main):012:0> end => :mm irb(main):013:0> mm("hello", "world") { |x| puts x } "hello" world => ["world"] irb(main):014:0>

Tienes *n y &blc en el orden incorrecto en la lista de parámetros.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda