From: Johan Holmberg Date: 2004-06-04T01:08:53+09:00 Subject: entries / to_a Hi ! The Enumerable module defines the two methods "entries" and "to_a" as synonyms for the same thing. Some classes that mix-in Enumerable re-implement "to_a" but *not* "entries". Sometimes this is ok, but in the case of the Array class I think this is a bad thing. An example: require "benchmark" arr = [11, 22, 33] * 1_000_000 Benchmark.bm(10) do |x| x.report("to_a") { arr.to_a.length } x.report("entries") { arr.entries.length } end This gives the following output on my Linux machine with an up-to-date version of Ruby from CVS: $ ruby test-entries.rb user system total real to_a 0.000000 0.000000 0.000000 ( 0.000047) entries 2.560000 0.020000 2.580000 ( 2.587834) The "entries" method return a newly created array even thou we started with an array. We could have returned the original array and saved some time (this is what "to_a" does). I added the following patch to "array.c": --- array.c.orig 2004-06-03 14:26:18.000000000 +0200 +++ array.c 2004-06-03 14:29:16.000000000 +0200 @@ -3001,6 +3001,7 @@ rb_define_method(rb_cArray, "to_s", rb_ary_to_s, 0); rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0); rb_define_method(rb_cArray, "to_a", rb_ary_to_a, 0); + rb_define_alias(rb_cArray, "entries", "to_a"); rb_define_method(rb_cArray, "to_ary", rb_ary_to_ary_m, 0); rb_define_method(rb_cArray, "frozen?", rb_ary_frozen_p, 0); Then I re-ran the testsuites I know about. The new faster version seemed to pass all tests so I assume that Ruby *doesn't require* that we should get a copy. Wouldn't it be a good idea to add this new behaviour to Ruby? I see no point in doing the copying. /Johan Holmberg