Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > item order in Object#methods

Reply
Thread Tools

item order in Object#methods

 
 
Andrea Fazzi
Guest
Posts: n/a
 
      12-18-2006
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


 
Reply With Quote
 
 
 
 
Rob Biedenharn
Guest
Posts: n/a
 
      12-18-2006

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
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
 
Andrea Fazzi
Guest
Posts: n/a
 
      12-18-2006
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

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      12-18-2006
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
(E-Mail Removed)



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
embedding python: simulating sequence get item / set item methods Johannes Zellner Python 1 01-17-2006 01:09 AM
XHTML block item inside and inline item tj.tzavaras@gmail.com HTML 2 04-28-2005 03:42 PM
wishlist item: itertools.partition (WAS: Wishlist item: itertools.flatten) Steven Bethard Python 0 03-12-2005 07:50 PM
Display Related Item or Add Item ssoss ASP .Net 2 09-18-2003 11:35 PM
item[LENGTH] vs. *item and malloc() root C Programming 2 08-21-2003 07:07 PM



Advertisments