# Item order in Object#methods

**URL:** <https://rubytalk.org/t/item-order-in-object-methods/33827>\
**Category:** ruby-talk\
**Created:** [18 December 2006 16:45 UTC](https://rubytalk.org/t/item-order-in-object-methods/33827 "2006-12-18T16:45:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Kistner](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@Gavin\_Kistner](https://rubytalk.org/u/Gavin_Kistner)\
**Post date:** [18 December 2006 16:45 UTC](https://rubytalk.org/t/item-order-in-object-methods/33827/1 "2006-12-18T16:45:55Z")

</div>

> Rob Biedenharn 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  
&nbsp;&nbsp;def track\_methods  
&nbsp;&nbsp;&nbsp;&nbsp;@methods\_in\_order =   
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :methods\_in\_order  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def method\_added( method\_name )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@methods\_in\_order \<\< method\_name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

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

class Bar  
&nbsp;&nbsp;include Foo  
&nbsp;&nbsp;track\_methods  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;init  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def init\_1; puts "Calling init\_1!"; end  
end

class Bar  
&nbsp;&nbsp;def init\_2; puts "Calling init\_2!"; end  
&nbsp;&nbsp;def init\_3; puts "Calling init\_3!"; end  
&nbsp;&nbsp;def init\_4; puts "Calling init\_4!"; end  
end

#init\_2.rb  
require 'track\_methods'  
require 'init'  
class Bar  
&nbsp;&nbsp;def init\_5; puts "Calling init\_5!"; end  
&nbsp;&nbsp;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!

> **···**
>
> From: Andrea Fazzi [mailto:andrea.fazzi@alca.le.it]

---

<div class="post-metadata">

**Author:** ![Andrea\_Fazzi](https://avatars.discourse-cdn.com/v4/letter/a/dc4da7/32.png) [@Andrea\_Fazzi](https://rubytalk.org/u/Andrea_Fazzi)\
**Post date:** [18 December 2006 17:27 UTC](https://rubytalk.org/t/item-order-in-object-methods/33827/2 "2006-12-18T17:27:29Z")

</div>

Gavin Kistner wrote:

> From: Andrea Fazzi [mailto:andrea.fazzi@alca.le.it]  
> &nbsp;&nbsp;
> 
> > Rob Biedenharn wrote:  
> > &nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > > If it's important, why not sort them first:  
> > > &nbsp;&nbsp;&nbsp;self.methods.sort.collect { ... }  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > 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.  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > 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
