From: Gavin Kistner Date: 2006-12-19T01:45:55+09:00 Subject: Re: item order in Object#methods From: Andrea Fazzi [mailto:andrea.fazzi@alca.le.it] > 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 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!