# Enumberable fails for class Subclass \< Array; def enum

**URL:** <https://www.ruby-forum.com/t/enumberable-fails-for-class-subclass-array-def-enum/97741>\
**Category:** Ruby\
**Created:** [July 10, 2006, 8:08am UTC](https://www.ruby-forum.com/t/enumberable-fails-for-class-subclass-array-def-enum/97741 "2006-07-10T08:08:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laza](https://www.ruby-forum.com/u/Laza)\
**Post date:** [July 10, 2006, 8:08am UTC](https://www.ruby-forum.com/t/enumberable-fails-for-class-subclass-array-def-enum/97741/1 "2006-07-10T08:08:15Z")

</div>

Array includes Enumerable. But when a subclass of Array redefines  
each(), then Enumerable still refers to the original Array.each(). Why  
is that?  
For example:

class RangeUnion \< Array  
def each  
super{|x| x.each{|i| yield i} }  
end  
end

# u = RangeUnion[1…3, 11…13, “a”…“c”]

# u.each{|x| print "#{x} "} # THIS WORKS

# #=\> 1 2 3 11 12 13 a b c

# 

# u.collect{|x| x} # THIS FAILS!!!

# #=\> [1, 2, 3, 11, 12, 13, “a”, “b”, “c”] # expected

# #=\> [1…3, 11…13, “a”…“c”] # actual

In the example above, each() is redefined. But collect() does not use  
the redefined each()!!! It uses Array.each(). Subclassing from some  
other class works fine:

class RangeUnion \< NotAnArray; … # gives expected result

It seems that Enumerable is hardwired (built in) in Array. But subclass  
should be able to override it. Why isn’t that possible?

-Laza

---

<div class="post-metadata">

**Author:** ![Laza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laza](https://www.ruby-forum.com/u/Laza)\
**Post date:** [July 10, 2006, 8:50am UTC](https://www.ruby-forum.com/t/enumberable-fails-for-class-subclass-array-def-enum/97741/2 "2006-07-10T08:50:52Z")

</div>

Hi,

At Mon, 10 Jul 2006 15:05:11 +0900,  
Laza wrote in [ruby-talk:201045]:

> class RangeUnion \< Array  
> define\_method(:collect, Enumerable.instance\_method(:collect))

---

<div class="post-metadata">

**Author:** ![Laza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laza](https://www.ruby-forum.com/u/Laza)\
**Post date:** [July 10, 2006, 11:47am UTC](https://www.ruby-forum.com/t/enumberable-fails-for-class-subclass-array-def-enum/97741/3 "2006-07-10T11:47:04Z")

</div>

Laza wrote:

> other class works fine:
> 
> class RangeUnion \< NotAnArray; … # gives expected result
> 
> It seems that Enumerable is hardwired (built in) in Array. But subclass  
> should be able to override it. Why isn’t that possible?

I’ve experienced problems myself when trying to redefine some of  
Enumerable’s methods. Apparently Array uses its own #collect and #map  
methods, plus some others. Maybe if you explicitly re-include Enumerable  
into your subclass?

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![Laza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laza](https://www.ruby-forum.com/u/Laza)\
**Post date:** [July 10, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/enumberable-fails-for-class-subclass-array-def-enum/97741/4 "2006-07-10T15:44:25Z")

</div>

Laza wrote:

> Array includes Enumerable. But when a subclass of Array redefines  
> each(), then Enumerable still refers to the original Array.each(). Why  
> is that?

Your confusion is understandable but you’ve got the problem backward:  
that  
behavior is not because Enumerable uses Array’s each, it’s because Array  
defines  
its own version of collect. Look at Array’s local (non-inherited)  
methods:

[].public\_methods(false).sort  
=\> ["&", “\*”, “+”, “-”, “\<\<”, “\<=\>”, “==”, “[]”, “[]=”, “assoc”, “at”,  
“clear”,  
“collect”, “collect!”, “compact”, “compact!”, “concat”, “delete”,  
“delete\_at”,  
“delete\_if”, “each”, “each\_index”, “empty?”, “eql?”, “fetch”, “fill”,  
“first”,  
“flatten”, “flatten!”, “frozen?”, “hash”, “include?”, “index”,  
“indexes”,  
“indices”, “insert”, “inspect”, “join”, “last”, “length”, “map”, “map!”,  
“nitems”, “pack”, “pop”, “push”, “rassoc”, “reject”, “reject!”,  
“replace”,  
“reverse”, “reverse!”, “reverse\_each”, “rindex”, “select”, “shift”,  
“size”,  
“slice”, “slice!”, “sort”, “sort!”, “to\_a”, “to\_ary”, “to\_s”,  
“transpose”,  
“uniq”, “uniq!”, “unshift”, “values\_at”, “zip”, “|”]

I suppose it was done that way for optimization. Why do you need to  
subclass  
Array, anyway?

Daniel

---

<div class="post-metadata">

**Author:** ![Laza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laza](https://www.ruby-forum.com/u/Laza)\
**Post date:** [July 10, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/enumberable-fails-for-class-subclass-array-def-enum/97741/5 "2006-07-10T19:38:28Z")

</div>

re-including Enumerable does not help. I re-implemented the class by  
having an Array member variable instead of inheriting from Array class.  
Apparently, not all classes are created equal in Ruby.
