# Re: item order in Object#methods

**URL:** <https://www.ruby-forum.com/t/re-item-order-in-object-methods/79742>\
**Category:** Ruby\
**Created:** [December 18, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/re-item-order-in-object-methods/79742 "2006-12-18T17:47:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 18, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/re-item-order-in-object-methods/79742/1 "2006-12-18T17:47:42Z")

</div>

From: Andrea F. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> 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.

I’m moderately sure that Ruby doesn’t internally keep track of the order  
in which classes were defined, or the order in which methods are added  
to them. But that doesn’t mean you can’t do it yourself with a little  
bit of work.

#track\_methods.rb  
class Module  
def track\_methods  
@methods\_in\_order = []  
class \<\< self  
attr\_reader :methods\_in\_order  
def method\_added( method\_name )  
@methods\_in\_order \<\< method\_name  
end  
end  
end  
end

# init.rb

module Foo  
def init  
self.class.methods\_in\_order.each do |init\_meth|  
unless init\_meth==:initialize  
self.send( init\_meth )  
end  
end  
end  
end

class Bar  
include Foo  
track\_methods  
def initialize  
init  
end  
def init\_1; puts “Calling init\_1!”; end  
end

class Bar  
def init\_2; puts “Calling init\_2!”; end  
def init\_3; puts “Calling init\_3!”; end  
def init\_4; puts “Calling init\_4!”; end  
end

#init\_2.rb  
require ‘track\_methods’  
require ‘init’  
class Bar  
def init\_5; puts “Calling init\_5!”; end  
def init\_6; puts “Calling init\_6!”; end  
end

Bar.new  
#=\> Calling init\_1!  
#=\> Calling init\_2!  
#=\> Calling init\_3!  
#=\> Calling init\_4!  
#=\> Calling init\_5!  
#=\> Calling init\_6!

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 18, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/re-item-order-in-object-methods/79742/2 "2006-12-18T18:27:52Z")

</div>

Gavin K. wrote:

> > init\_2, etc. were just an example.
> 
> I’m moderately sure that Ruby doesn’t internally keep track of the order  
> in which classes were defined, or the order in which methods are added  
> to them. But that doesn’t mean you can’t do it yourself with a little  
> bit of work.

Thank you Gavin, your implementation solved my problem!  
I like very very much your solution! 🙂

Thanks again.  
Andrea
