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

224
Views
¿Cómo puedo crear un Generador en Ruby?

¿Cómo puedo hacer esta parte del código de Javascript a Ruby? Me gustaría traducir de Javascript a Ruby esta función:

 function* generate(arr) { console.log('AAAA'+ arr) const A = Array.from(arr); // copy const len = A.length; const c = new Array(len).fill(0); let i = 0; yield A; while (i < len) { if (c[i] < i) { let j = i & 1 ? c[i] : 0; if (A[i] != A[j]) { [A[j], A[i]] = [A[i], A[j]]; yield A; } c[i]++; i = 0; } else { c[i] = 0; i++; } } };

Estoy usando así esa función:

 function* definition(data) { const state = data.map(v => generate(v));

y la función de definición que estoy usando así:

 arr= [ ['1','2','1','2','1'], ['2','2','2','4','4'], ] state = definition(arr) console.log('STATE',state)

esta consola obteniendo esto:

ESTADO[generador de objetos],[generador de objetos],[generador de objetos] la consola me muestra esto:

 AAAA1,1,1,2,1 AAAA3,2,4,4,4 AAAA6,6,7,8,7

Estaba intentando este es mi intento:

 def generate(arr) a = arr.clone len = a.size c = new Array(len).fill(0) let i = 0 yield a while i < len if(c[i] < i) let j = i&1 ? c[i] : 0 if (a[i] != a[j]) a[j], a[i] = [a[i], a[j]] yield a end c[i]++ i = 0 else c[i] = 0 i++ end end end

En Ruby estoy usando así:

 def definition(data) state = data.map { |v| generate(v) }

Pero necesito una función* en Ruby para crear un generador.

también, este new Array(len).fill(0) está bien?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Aquí la función del generador JS

 function* generator() { let i = 0 while (true) { yield i++ } } gen = generator() console.log(gen.next().value) // will print 0 console.log(gen.next().value) // will print 1 console.log(gen.next().value) // will print 2 // etc.

Y aquí Ruby análogo a la comprensión.

 def generator Enumerator.new do |enum| i = 0 loop do enum.yield i i += 1 end end end gen = generator puts gen.next # will print 0 puts gen.next # will print 1 puts gen.next # will print 2 # etc.
about 4 years ago · Juan Pablo Isaza Report

0

generator = Enumerator.new do |yielder| 5.times do 5.times do |i| yielder << i end end end generator.to_a # => [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4] # Or use different methods from Enumerator or Enumerable # Or make a function that returns this Enumerator for # closest approximation of what you do with JS

En general, Ruby no tiene el concepto de funciones * o async . Debe pensar en ellos como si TODAS las funciones de Ruby fueran async y TODAS las llamadas estuvieran en await . Lo que esto significa es que, a diferencia de JavaScript, en Ruby puede cancelar la ejecución en cualquier momento, utilizando una interfaz de bajo nivel de Fiber o abstracciones de alto nivel como el mencionado Enumerator::Generator (creado con Enumerator.new).

about 4 years ago · Juan Pablo Isaza 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!