# Reflection method name order

**URL:** <https://www.ruby-forum.com/t/reflection-method-name-order/154186>\
**Category:** Ruby\
**Created:** [November 26, 2008, 8:37am UTC](https://www.ruby-forum.com/t/reflection-method-name-order/154186 "2008-11-26T08:37:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 26, 2008, 8:37am UTC](https://www.ruby-forum.com/t/reflection-method-name-order/154186/1 "2008-11-26T08:37:31Z")

</div>

Can I depend on .methods returning methods in the order they are written  
in the class source code?

I would assume Unit::Test counts on that, and playing around with this  
test seems to indicate that order is retained, but not sure if I’m just  
getting lucky. (Seems weird that order is retained, yet the names are  
dispersed among all the other inherited methods of the class, rather  
than grouped at the end or something.)

class Matchers  
def match\_first  
end  
def match\_second  
end  
def match\_another  
end  
def match\_last\_one  
end  
end

x = Matchers.new

matchers = []  
x.methods.each do |method\_name|  
matchers \<\< method\_name if method\_name.scan(/^match\_/)[0]  
end

p matchers

# =\> [“match\_first”, “match\_second”, “match\_another”, “match\_last\_one”]

Thx

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 26, 2008, 8:47am UTC](https://www.ruby-forum.com/t/reflection-method-name-order/154186/2 "2008-11-26T08:47:08Z")

</div>

Alle Wednesday 26 November 2008, Greg W. ha scritto:

> def match\_first
> 
> – gw  
> I don’t think you can depend on it. Look at this:

class C  
def x;end  
def y;end  
def a;end  
def z;end  
end  
p C.new.methods.grep(/^\w$/)  
#=\> [“y”, “x”, “z”, “a”]

This is with ruby 1.8.7 and linux. By the way, why do you think  
Test::Unit  
relies on the definition order of the methods?

Stefano
