Tenía curiosidad por saber más diferencias entre [] y Array.new y {} y Hash.new¿Diferencias entre literales y constructores? ([] vs Array.new y {} vs Hash.new)
Corrí los mismos puntos de referencia y parece como las abreviaturas son ganadores
require 'benchmark'
many = 500000
Benchmark.bm do |b|
b.report("[] \t") {many.times { [].object_id }}
b.report("Array.new \t") { many.times { Array.new.object_id }}
b.report("{} \t") {many.times { {}.object_id }}
b.report("Hash.new\t") { many.times { Hash.new.object_id }}
end
user system total real
[] 0.080000 0.000000 0.080000 ( 0.079287)
Array.new 0.180000 0.000000 0.180000 ( 0.177105)
{} 0.080000 0.000000 0.080000 ( 0.079467)
Hash.new 0.260000 0.000000 0.260000 ( 0.264796)
personalmente me gusta usar de [] la taquigrafía y {}, el código parece tan fresco y fácil de leer.
Cualquier otro puntero ¿cuál es la diferencia entre ellos? ¿Qué sucede detrás de la escena que lo hace tan mejor, y sugerencias si hay alguna cuándo usarlo?
Encontré este link pero estaba buscando obtener más información.
aplausos.
'Hash.new (0)'? –