item order in Object#methods

Discussion in 'Ruby' started by Andrea Fazzi, Dec 18, 2006.

  1. Andrea Fazzi

    Andrea Fazzi Guest

    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
    Andrea Fazzi, Dec 18, 2006
    #1
    1. Advertising

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


    If it's important, why not sort them first:
    self.methods.sort.collect { ... }

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Dec 18, 2006
    #2
    1. Advertising

  3. Andrea Fazzi

    Andrea Fazzi Guest

    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
    Andrea Fazzi, Dec 18, 2006
    #3
  4. 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


    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 Biedenharn, Dec 18, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ssoss

    Display Related Item or Add Item

    ssoss, Sep 17, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    484
    ssoss
    Sep 19, 2003
  2. Johannes Zellner
    Replies:
    1
    Views:
    498
    Alex Martelli
    Jan 17, 2006
  3. Tom Counsell
    Replies:
    3
    Views:
    90
    Florian Frank
    Sep 13, 2004
  4. Nikita Petrov
    Replies:
    2
    Views:
    110
    Gary Wright
    Apr 6, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    312
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page