The difference between #entries and #to_a in ruby

What is the main difference between the #entries and #to_a the Enumerable module in ruby. It seems the same result returns a Hash

 >> hash = {"name" => "foo" , "age" => "23"} => {"name"=>"foo", "age"=>"23"} >> hash.to_a => [["name","foo"],["age",23]] >> hash.entries => [["name","foo"],["age",23]] 
+6
source share
1 answer

Here is the difference (look at the result after # => ):

 h = {} h.method(:entries) # => #<Method: Hash(Enumerable)#entries> h.method(:to_a) # => #<Method: Hash#to_a> h.method(:entries).owner # => Enumerable h.method(:to_a).owner # => Hash 

Hash has an instance method #to_a , so it does not call Enumerable#to_a . But Hash does not have its own #entries method, so it calls Enumerable#entries , because Hash Enumerable module enabled.

 Hash.included_modules # => [Enumerable, Kernel] 

There is no difference between Enumerable#entries and Enumerable#to_a , as far as I can see, both work in a similar way using TracePoint :

 1. trace = TracePoint.new do |tp| 2. p [tp.lineno, tp.event, tp.defined_class,tp.method_id] 3. end 4. 5. trace.enable do 6. (1..2).entries 7. (1..2).to_a 8. end # >> [5, :b_call, nil, nil] # >> [6, :line, nil, nil] # >> [6, :line, nil, nil] # >> [6, :c_call, Enumerable, :entries] # >> [6, :c_call, Range, :each] # >> [6, :c_return, Range, :each] # >> [6, :c_return, Enumerable, :entries] # >> [7, :line, nil, nil] # >> [7, :line, nil, nil] # >> [7, :c_call, Enumerable, :to_a] # >> [7, :c_call, Range, :each] # >> [7, :c_return, Range, :each] # >> [7, :c_return, Enumerable, :to_a] # >> [8, :b_return, nil, nil] 

Yes, Hash#to_a faster than Enumerable#to_a .

Part - I

 require 'benchmark' class Hash remove_method :to_a end hsh = Hash[*1..1000] Benchmark.bm(10) do |b| b.report("Enumerable#to_a") { hsh.to_a } end # >> user system total real # >> Enumerable#to_a 0.000000 0.000000 0.000000 ( 0.000126) 

Part II

 require 'benchmark' hsh = Hash[*1..1000] Benchmark.bm(10) do |b| b.report("Hash#to_a") { hsh.to_a } end # >> user system total real # >> Hash#to_a 0.000000 0.000000 0.000000 ( 0.000095) 
+15
source

All Articles