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
If it's important, why not sort them first:
self.methods.sort.collect { ... }
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
···
On Dec 18, 2006, at 10:08 AM, Andrea Fazzi wrote:
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
...
So, which is the sort criteria of the array returned by Object#methods?
Thanks a lot!
Andrea
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.
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 Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
···
On Dec 18, 2006, at 10:32 AM, Andrea Fazzi wrote:
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.
Andrea