From: David A. Black on 6 Jun 2010 20:55 Hi -- On Mon, 7 Jun 2010, Rein Henrichs wrote: > In addition to the previously mentioned difference between #methods and > #instance_methods, #inject is actually a method on the Enumerable module, > which is mixed into Array (and other classes that have #each). The Enumerable > module is full of methods that can be used on "things that can enumerate > themselves", like Arrays, Hashes and Sets. Although a surprising (to me) number of Enumerable's instance methods are overridden in Array: irb(main):008:0> (Array.instance_methods(false) & Enumerable.instance_methods).sort => ["collect", "count", "cycle", "drop", "drop_while", "find_index", "first", "include?", "map", "reject", "reverse_each", "select", "sort", "take", "take_while", "to_a", "zip"] (That's 1.8.7 but the list is the same in 1.9.1.) David -- David A. Black, Senior Developer, Cyrus Innovation Inc. THE Ruby training with Black/Brown/McAnally COMPLEAT Coming to Chicago area, June 18-19, 2010! RUBYIST http://www.compleatrubyist.com
From: Rein Henrichs on 6 Jun 2010 21:22 On 2010-06-06 17:55:58 -0700, David A. Black said: > Hi -- > > On Mon, 7 Jun 2010, Rein Henrichs wrote: > >> In addition to the previously mentioned difference between #methods and >> #instance_methods, #inject is actually a method on the Enumerable module, >> which is mixed into Array (and other classes that have #each). The Enumerable >> module is full of methods that can be used on "things that can enumerate >> themselves", like Arrays, Hashes and Sets. > > Although a surprising (to me) number of Enumerable's instance methods > are overridden in Array: > > irb(main):008:0> (Array.instance_methods(false) & > Enumerable.instance_methods).sort > => ["collect", "count", "cycle", "drop", "drop_while", "find_index", > "first", "include?", "map", "reject", "reverse_each", "select", > "sort", "take", "take_while", "to_a", "zip"] > > (That's 1.8.7 but the list is the same in 1.9.1.) > > > David I suspect that the majority are overwritten for performance reasons, although some of them surprise me as well. A linked list, for instance, would have a constant time #count because that state is kept on the list object and doesn't require an O(n) enumeration of the members. -- Rein Henrichs http://puppetlabs.com http://reinh.com
From: RichardSchollar on 7 Jun 2010 00:29 Thanks everyone - I appreciate all the responses and it has clarified what I was seeing.
From: David A. Black on 7 Jun 2010 05:42 Hi -- On Mon, 7 Jun 2010, Rein Henrichs wrote: > On 2010-06-06 17:55:58 -0700, David A. Black said: > >> Hi -- >> >> On Mon, 7 Jun 2010, Rein Henrichs wrote: >> >>> In addition to the previously mentioned difference between #methods and >>> #instance_methods, #inject is actually a method on the Enumerable module, >>> which is mixed into Array (and other classes that have #each). The >>> Enumerable >>> module is full of methods that can be used on "things that can enumerate >>> themselves", like Arrays, Hashes and Sets. >> >> Although a surprising (to me) number of Enumerable's instance methods >> are overridden in Array: >> >> irb(main):008:0> (Array.instance_methods(false) & >> Enumerable.instance_methods).sort >> => ["collect", "count", "cycle", "drop", "drop_while", "find_index", >> "first", "include?", "map", "reject", "reverse_each", "select", >> "sort", "take", "take_while", "to_a", "zip"] >> >> (That's 1.8.7 but the list is the same in 1.9.1.) >> >> >> David > > I suspect that the majority are overwritten for performance reasons, although > some of them surprise me as well. > > A linked list, for instance, would have a constant time #count because that > state is kept on the list object and doesn't require an O(n) enumeration of > the members. I think it's a mixture of performance reasons and things like what it should return if there's no block (like Enumerable#map always returning an array vs. Array#map returning its receiver). Array is definitely the one that has the most overrides, with Hash coming in second at 6 and IO having none. David -- David A. Black, Senior Developer, Cyrus Innovation Inc. THE Ruby training with Black/Brown/McAnally COMPLEAT Coming to Chicago area, June 18-19, 2010! RUBYIST http://www.compleatrubyist.com
From: Robert Klemme on 7 Jun 2010 07:46 2010/6/7 David A. Black <dblack(a)rubypal.com>: > On Mon, 7 Jun 2010, Rein Henrichs wrote: > >> On 2010-06-06 17:55:58 -0700, David A. Black said: >> I suspect that the majority are overwritten for performance reasons, >> although some of them surprise me as well. >> >> A linked list, for instance, would have a constant time #count because >> that state is kept on the list object and doesn't require an O(n) >> enumeration of the members. > > I think it's a mixture of performance reasons and things like what it > should return if there's no block (like Enumerable#map always > returning an array vs. Array#map returning its receiver). Array is > definitely the one that has the most overrides, with Hash coming in > second at 6 and IO having none. David, I guess you meant to write "... vs. Array#map! returning its receiver"? Note the exclamation mark. Array#map returns a new Array on every invocation. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: libruby-nntp doesn't exist any more ? Next: Singleton classes and Namespce |