# Item order in Object#methods

**URL:** <https://www.ruby-forum.com/t/item-order-in-object-methods/79728>\
**Category:** Ruby\
**Created:** [December 18, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/item-order-in-object-methods/79728 "2006-12-18T16:09:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gendag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f04885/32.png) [@gendag](https://www.ruby-forum.com/u/gendag)\
**Post date:** [December 18, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/item-order-in-object-methods/79728/1 "2006-12-18T16:09:27Z")

</div>

Hi all,

please consider two files: init.rb and init\_2.rb. In init.rb put this  
bunch of code:

module Foo  
def init  
self.methods.collect { |meth| meth if meth =~ /init\_/ }.compact.each  
do |init\_meth|  
self.send(init\_meth.to\_sym)  
end  
end  
end

class Bar  
include Foo  
def initialize  
init  
end  
def init\_1  
puts “Method init\_1 called!”  
end  
end

class Bar  
def init\_2  
puts “Method init\_2 called!”  
end  
def init\_3  
puts “Method init\_3 called!”  
end  
def init\_4  
puts “Method init\_4 called!”  
end  
end

Then in init\_2.rb put another bunch of code:

require ‘init’

class Bar  
def init\_5  
puts “Method init\_5 called!”  
end  
def init\_6  
puts “Method init\_6 called!”  
end  
end

Bar.new

Now execute init\_2.rb. I get the following result:

Method init\_4 called!  
Method init\_5 called!  
Method init\_6 called!  
Method init\_1 called!  
Method init\_2 called!  
Method init\_3 called!

but I expected:

Method init\_1 called!  
Method init\_2 called!  
Method init\_3 called!  
Method init\_4 called!  
Method init\_5 called!  
Method init\_6 called!

So, which is the sort criteria of the array returned by Object#methods?

Thanks a lot!  
Andrea

---

<div class="post-metadata">

**Author:** ![gendag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f04885/32.png) [@gendag](https://www.ruby-forum.com/u/gendag)\
**Post date:** [December 18, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/item-order-in-object-methods/79728/2 "2006-12-18T16:21:30Z")

</div>

On Dec 18, 2006, at 10:08 AM, Andrea F. wrote:

> end  
> end  
> end
> 
> …
> 
> So, which is the sort criteria of the array returned by  
> Object#methods?
> 
> Thanks a lot!  
> Andrea

If it’s important, why not sort them first:  
self.methods.sort.collect { … }

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gendag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f04885/32.png) [@gendag](https://www.ruby-forum.com/u/gendag)\
**Post date:** [December 18, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/item-order-in-object-methods/79728/3 "2006-12-18T16:33:32Z")

</div>

Rob B. wrote:

> If it’s important, why not sort them first:  
> self.methods.sort.collect { … }

Because I’m interested to the methods’ definition order inside the class  
not to the alphabetical order of methods’ name. The names init\_1,  
init\_2, etc. were just an example.

Andrea

---

<div class="post-metadata">

**Author:** ![gendag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f04885/32.png) [@gendag](https://www.ruby-forum.com/u/gendag)\
**Post date:** [December 18, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/item-order-in-object-methods/79728/4 "2006-12-18T17:17:36Z")

</div>

On Dec 18, 2006, at 10:32 AM, Andrea F. wrote:

> Rob B. wrote:
> 
> > If it’s important, why not sort them first:  
> > self.methods.sort.collect { … }
> 
> Because I’m interested to the methods’ definition order inside the  
> class not to the alphabetical order of methods’ name. The names  
> init\_1, init\_2, etc. were just an example.
> 
> Andrea

I’d not be surprised to hear (from someone who actually knows rather  
than speculates 😉 that the methods are stored in a hash and there  
is no definite order to them. As for the order in which they were  
defined, that’s just the order in which they were encountered.

When I run the code from your first message:  
rab:ruby $ ruby init\_2.rb  
Method init\_3 called!  
Method init\_4 called!  
Method init\_5 called!  
Method init\_6 called!  
Method init\_1 called!  
Method init\_2 called!

It’s not even the same order as yours (although it is consistent when  
I run it multiple times). If the lookup is really hash-based,  
defining other methods could “shuffle” these around if the underlying  
hash table was expanded. (I’m using “hash” in its algorithmic sense,  
not a Ruby class.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
