A
Andrea Fazzi
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
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