# Alias

**URL:** <https://rubytalk.org/t/alias/73877>\
**Category:** ruby-talk\
**Created:** [9 November 2016 10:08 UTC](https://rubytalk.org/t/alias/73877 "2016-11-09T10:08:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)\
**Post date:** [9 November 2016 10:08 UTC](https://rubytalk.org/t/alias/73877/1 "2016-11-09T10:08:54Z")

</div>

Hello

def self.mymeth  
&nbsp;&nbsp;&nbsp;'yes'  
end

alias yesmeth mymeth # error

How can I make an alias for a class method or a method in another class/namespace?  
How do I adress the main-Namespace (in which class / namespace are the methods not defined inside any class?)

thanks,  
Opti

---

<div class="post-metadata">

**Author:** ![Andy\_Jones](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/andy_jones/32/1829_2.png) [@Andy\_Jones](https://rubytalk.org/u/Andy_Jones)\
**Post date:** [9 November 2016 10:22 UTC](https://rubytalk.org/t/alias/73877/2 "2016-11-09T10:22:06Z")

</div>

> def self.mymeth  
> &nbsp;&nbsp;&nbsp;'yes'  
> end
> 
> alias yesmeth mymeth # error
> 
> How can I make an alias for a class method

&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def mymeth  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'yes'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias yesmeth mymeth  
&nbsp;&nbsp;&nbsp;&nbsp;end

> or a method in another class/namespace?

Eek. You can't use alias to do that AFAIK and it would be really confusing if you could? I'd hate to have to debug that code.

OTOH if you have a class Foo with a method 'one' which you want to call 'two' inside class Bar:

&nbsp;&nbsp;&nbsp;&nbsp;class Bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(foo); @foo = foo; end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def two; @foo.one; end  
&nbsp;&nbsp;&nbsp;&nbsp;end

> How do I adress the main-Namespace (in which class / namespace are the  
> methods not defined inside any class?)

Again, this sounds like a terrible idea to me -- I don't want the code I require to be altering the main namespace. I'm sure someone here can/will come up with a way to do it, but I really think you should find a better way to do what you want.

Click here to view Company Information and Confidentiality Notice.\<[http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt](http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt);

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [9 November 2016 10:20 UTC](https://rubytalk.org/t/alias/73877/3 "2016-11-09T10:20:43Z")

</div>

Sorry for a brief reply (I'm on my phone). How about something like this?

> **···**
>
> ~~~  
> class \<\< self  
> &nbsp;&nbsp;def mymeth  
> &nbsp;&nbsp;&nbsp;&nbsp;'yes'  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;alias yesmeth mymeth  
> end  
> ~~~
> 
> Try looking up "singleton class" if you want to research it yourself.
> 
> Cheers  
> Matty
> 
> On 09/11/2016, Die Optimisten \<inform@die-optimisten.net\> wrote:
> 
> > Hello
> > 
> > def self.mymeth  
> > &nbsp;&nbsp;&nbsp;'yes'  
> > end
> > 
> > alias yesmeth mymeth # error
> > 
> > How can I make an alias for a class method or a method in another  
> > class/namespace?  
> > How do I adress the main-Namespace (in which class / namespace are the  
> > methods not defined inside any class?)
> > 
> > thanks,  
> > Opti
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> 
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)\
**Post date:** [11 November 2016 12:13 UTC](https://rubytalk.org/t/alias/73877/4 "2016-11-11T12:13:18Z")

</div>

Hello,

What is the name of the Class/Namespace if not having defined any?  
(what does self reference to, if working on the top, without class MyClass end ? )

thanks,  
Opti

---

<div class="post-metadata">

**Author:** ![Michael\_Schwarze](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/michael_schwarze/32/1876_2.png) [@Michael\_Schwarze](https://rubytalk.org/u/Michael_Schwarze)\
**Post date:** [11 November 2016 12:56 UTC](https://rubytalk.org/t/alias/73877/5 "2016-11-11T12:56:01Z")

</div>

Hi Opti,

> **···**
>
> > Am 11.11.2016 um 13:13 schrieb Die Optimisten \<inform@die-optimisten.net\>:  
> > What is the name of the Class/Namespace if not having defined any? (what does self reference to, if working on the top, without class MyClass end ? )
> 
> I think it should be Object:
> 
> ~~~  
> &nbsp;&nbsp;&nbsp;2.3.1 :001 \> self  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> main  
> &nbsp;&nbsp;&nbsp;2.3.1 :002 \> self.instance\_of?(Object)  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> true  
> ~~~
> 
> Cheers,  
> Michael

---

<div class="post-metadata">

**Author:** ![Andy\_Jones](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/andy_jones/32/1829_2.png) [@Andy\_Jones](https://rubytalk.org/u/Andy_Jones)\
**Post date:** [11 November 2016 13:45 UTC](https://rubytalk.org/t/alias/73877/6 "2016-11-11T13:45:52Z")

</div>

> I think it should be Object:
> 
> ~~~  
> &nbsp;&nbsp;&nbsp;2.3.1 :001 \> self  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> main  
> &nbsp;&nbsp;&nbsp;2.3.1 :002 \> self.instance\_of?(Object)  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> true  
> ~~~

As you can see from your own example, it's 'main'. Object is the not-quite-final parent of all other objects. But then things get weird. The idea of a class hierarchy breaks up a little right at the top.

&nbsp;&nbsp;&nbsp;&nbsp;[13] pry(main)\> Object.ancestors  
&nbsp;&nbsp;&nbsp;&nbsp;=\> [Object, PP::ObjectMixin, Kernel, BasicObject]

&nbsp;&nbsp;&nbsp;&nbsp;[14] pry(main)\> Kernel.ancestors  
&nbsp;&nbsp;&nbsp;&nbsp;=\> [Kernel]

&nbsp;&nbsp;&nbsp;&nbsp;[15] pry(main)\> BasicObject.ancestors  
&nbsp;&nbsp;&nbsp;&nbsp;=\> [BasicObject]

Click here to view Company Information and Confidentiality Notice.\<[http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt](http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt);

---

<div class="post-metadata">

**Author:** ![Michael\_Schwarze](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/michael_schwarze/32/1876_2.png) [@Michael\_Schwarze](https://rubytalk.org/u/Michael_Schwarze)\
**Post date:** [11 November 2016 14:33 UTC](https://rubytalk.org/t/alias/73877/7 "2016-11-11T14:33:48Z")

</div>

Weird indeed:

> **···**
>
> Am 11.11.2016 um 14:45 schrieb Andy Jones \<Andy.Jones@jameshall.co.uk\>:
> 
> > As you can see from your own example, it's 'main'. Object is the not-quite-final parent of all other objects. But then things get weird.
> 
> ~~~  
> 2.3.1 :001 \> self.class  
> =\> Object  
> ~~~

---

<div class="post-metadata">

**Author:** ![Andy\_Jones](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/andy_jones/32/1829_2.png) [@Andy\_Jones](https://rubytalk.org/u/Andy_Jones)\
**Post date:** [14 November 2016 09:24 UTC](https://rubytalk.org/t/alias/73877/8 "2016-11-14T09:24:32Z")

</div>

> Weird indeed:
> 
> ~~~  
> 2.3.1 :001 \> self.class  
> =\> Object  
> ~~~

You are assuming that the top-level namespace in which the object hierarchy runs is itself an object in that hierarchy. I suppose ideally that would be the case. But I don't think it's true in fact.

You can traverse the object hierarchy upwards, sure, but that doesn't mean you will find that main object.

Click here to view Company Information and Confidentiality Notice.\<[http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt](http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt);

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [14 November 2016 12:29 UTC](https://rubytalk.org/t/alias/73877/9 "2016-11-14T12:29:02Z")

</div>

> > Weird indeed:
> > 
> > ~~~  
> > 2.3.1 :001 \> self.class  
> > =\> Object  
> > ~~~
> 
> You are assuming that the top-level namespace in which the object hierarchy runs is itself an object in that hierarchy. I suppose ideally that would be the case. But I don't think it's true in fact.

Let's see

irb(main):001:0\> Object.instance\_method :abcd  
NameError: undefined method `abcd' for class `Object'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `instance\_method'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/bin/irb:11:in `\<main\>'  
irb(main):002:0\> def abcd; end  
=\> :abcd  
irb(main):003:0\> Object.instance\_method :abcd  
=\> #\<UnboundMethod: Object#abcd\>  
irb(main):004:0\> Object.private\_instance\_methods.grep /abcd/  
=\> [:abcd]

So the top level namespace is Object. Since Object is a class it is  
also an instance of itself:

irb(main):005:0\> Object.class  
=\> Class  
irb(main):006:0\> Object.class.ancestors  
=\> [Class, Module, Object, Kernel, BasicObject]  
irb(main):007:0\> Object.is\_a? Object  
=\> true  
irb(main):008:0\> Object === Object  
=\> true

It's a bit mind boggling - but just if you think about it too long. 😉

> You can traverse the object hierarchy upwards, sure, but that doesn't mean you will find that main object.

Especially since there is a class hierarchy only and not an object  
hierarchy. 🙂

Cheers

robert

> **···**
>
> On Mon, Nov 14, 2016 at 10:24 AM, Andy Jones \<Andy.Jones@jameshall.co.uk\> wrote:
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Andy\_Jones](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/andy_jones/32/1829_2.png) [@Andy\_Jones](https://rubytalk.org/u/Andy_Jones)\
**Post date:** [14 November 2016 13:19 UTC](https://rubytalk.org/t/alias/73877/10 "2016-11-14T13:19:42Z")

</div>

> So the top level namespace is Object. Since Object is a class it is  
> also an instance of itself:

Yes, that's the top of the object hierarchy, but not the 'main object'. Main is an instance \*of\* Object (?) ... but then, so is everything else.

You might want to look at this discussion on SO: [program entry point - What is "main" in Ruby? - Stack Overflow](http://stackoverflow.com/questions/917811/what-is-main-in-ruby#917842)

Or this one: [metaprogramming - Inconsistency about ruby 'main' object - Stack Overflow](http://stackoverflow.com/questions/11474610/inconsistency-about-ruby-main-object)

Or this blog post: [Ruby's Top Self Object — SitePoint](https://www.sitepoint.com/rubys-top-self-object/)

> Especially since there is a class hierarchy only and not an object  
> hierarchy. 🙂

If that were true then you would not have been able to type 'self.class' as you did.

Or, indeed:

&nbsp;&nbsp;&nbsp;&nbsp;[1] pry(main)\> class Foo; end  
&nbsp;&nbsp;&nbsp;&nbsp;=\> nil

&nbsp;&nbsp;&nbsp;&nbsp;[2] pry(main)\> module Bar; end  
&nbsp;&nbsp;&nbsp;&nbsp;=\> nil

&nbsp;&nbsp;&nbsp;&nbsp;[3] pry(main)\> x = Foo.new.extend(Bar)  
&nbsp;&nbsp;&nbsp;&nbsp;=\> #\<Foo:0x000000037df598\>

&nbsp;&nbsp;&nbsp;&nbsp;[4] pry(main)\> x.class  
&nbsp;&nbsp;&nbsp;&nbsp;=\> Foo

&nbsp;&nbsp;&nbsp;&nbsp;[5] pry(main)\> x.class.ancestors  
&nbsp;&nbsp;&nbsp;&nbsp;=\> [Foo, Object, PP::ObjectMixin, Kernel, BasicObject]

&nbsp;&nbsp;&nbsp;&nbsp;[6] pry(main)\> x.singleton\_class  
&nbsp;&nbsp;&nbsp;&nbsp;=\> #\<Class:#\<Foo:0x000000037df598\>\>

&nbsp;&nbsp;&nbsp;&nbsp;[7] pry(main)\> x.singleton\_class.ancestors  
&nbsp;&nbsp;&nbsp;&nbsp;=\> [#\<Class:#\<Foo:0x000000037df598\>\>, Bar, Foo, Object, PP::ObjectMixin, Kernel, BasicObject]

Classes are also objects. There is a hierarchy of objects, some of which are classes...

Click here to view Company Information and Confidentiality Notice.\<[http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt](http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt);

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [14 November 2016 14:52 UTC](https://rubytalk.org/t/alias/73877/11 "2016-11-14T14:52:43Z")

</div>

> > So the top level namespace is Object. Since Object is a class it is  
> > also an instance of itself:
> 
> Yes, that's the top of the object hierarchy, but not the 'main object'. Main is an instance \*of\* Object (?) ... but then, so is everything else.

Only classes and modules constitute namespaces in Ruby. And, as I have  
shown, any method defined on top level is a private instance method of  
Object. So if anything, Object is the namespace.

But we are not actually invoking a method on top level as  
Object::meth() and actually we can't. Methods defined on top level are  
made available to almost all (because of BasicObject) instances via  
inheritance.

> You might want to look at this discussion on SO: [program entry point - What is "main" in Ruby? - Stack Overflow](http://stackoverflow.com/questions/917811/what-is-main-in-ruby#917842)
> 
> Or this one: [metaprogramming - Inconsistency about ruby 'main' object - Stack Overflow](http://stackoverflow.com/questions/11474610/inconsistency-about-ruby-main-object)
> 
> Or this blog post: [Ruby's Top Self Object — SitePoint](https://www.sitepoint.com/rubys-top-self-object/)
> 
> > Especially since there is a class hierarchy only and not an object  
> > hierarchy. 🙂
> 
> If that were true then you would not have been able to type 'self.class' as you did.

I am able to execute "self.class" because #class is an instance method  
of Kernel which is included by Object. So all instances of Object and  
sub classes inherit this method via the class / module hierarchy.

Of course, you can create arbitrary hierarchies of objects by  
establishing references between them. For the sake of inheritance  
there is only a class / module hierarchy.

> Or, indeed:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[1] pry(main)\> class Foo; end  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> nil
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[2] pry(main)\> module Bar; end  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> nil
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[3] pry(main)\> x = Foo.new.extend(Bar)  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> #\<Foo:0x000000037df598\>
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[4] pry(main)\> x.class  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> Foo
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[5] pry(main)\> x.class.ancestors  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> [Foo, Object, PP::ObjectMixin, Kernel, BasicObject]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[6] pry(main)\> x.singleton\_class  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> #\<Class:#\<Foo:0x000000037df598\>\>
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[7] pry(main)\> x.singleton\_class.ancestors  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> [#\<Class:#\<Foo:0x000000037df598\>\>, Bar, Foo, Object, PP::ObjectMixin, Kernel, BasicObject]
> 
> Classes are also objects.

Yes.

> There is a hierarchy of objects, some of which are classes...

That statement does not make sense to me as indicated above. The  
hierarchical relationship between classes is an inheritance  
relationship via #superclass. The relationship between objects and  
classes is that of instantiation via #instance\_of?.

Cheers

robert

> **···**
>
> On Mon, Nov 14, 2016 at 2:19 PM, Andy Jones \<Andy.Jones@jameshall.co.uk\> wrote:
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Andy\_Jones](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/andy_jones/32/1829_2.png) [@Andy\_Jones](https://rubytalk.org/u/Andy_Jones)\
**Post date:** [14 November 2016 15:14 UTC](https://rubytalk.org/t/alias/73877/12 "2016-11-14T15:14:30Z")

</div>

> That statement does not make sense to me as indicated above.

I think perhaps our mental models of Ruby are incompatible. One final thought.

> The  
> hierarchical relationship between classes is an inheritance  
> relationship via #superclass. The relationship between objects and  
> classes is that of instantiation via #instance\_of?.

What, then, is this?

> &nbsp;&nbsp;&nbsp;&nbsp;[1] pry(main)\> class Foo; end  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> nil
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[2] pry(main)\> module Bar; end  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> nil
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[3] pry(main)\> x = Foo.new.extend(Bar)  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> #\<Foo:0x000000037df598\>
> 
> [...]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[7] pry(main)\> x.singleton\_class.ancestors  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> [#\<Class:#\<Foo:0x000000037df598\>\>, Bar, Foo, Object, PP::ObjectMixin, Kernel, BasicObject]

When Ruby tries to find a method, it walks up a \*\*single hierarchy\*\* of objects. It starts with the current object, then looks in the singleton class of the object, then the parents of that singleton class, then the parent class of the object, then the singleton class of that class and so on: one hierarchy involving the current object and any other number of classes, modules, singleton classes and (admittedly this would be pretty weird?) other objects.

You can, of course, view things in terms of a separate class inheritance hierarchy if you wish. In other languages this is literally true. But really, Ruby isn't \_quite\_ working like that.

Admittedly the difference is a subtle one, and most of the time it's not important. Unless you are mucking around with the singleton class (aka "eigenclass") it probably doesn't matter at all.

Click here to view Company Information and Confidentiality Notice.\<[http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt](http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt);

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [14 November 2016 19:47 UTC](https://rubytalk.org/t/alias/73877/13 "2016-11-14T19:47:04Z")

</div>

> > That statement does not make sense to me as indicated above.
> 
> I think perhaps our mental models of Ruby are incompatible. One final thought.

I am suspecting it is rather different usage of terms.

> > The  
> > hierarchical relationship between classes is an inheritance  
> > relationship via #superclass. The relationship between objects and  
> > classes is that of instantiation via #instance\_of?.
> 
> What, then, is this?
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;[1] pry(main)\> class Foo; end  
> > &nbsp;&nbsp;&nbsp;&nbsp;=\> nil
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;[2] pry(main)\> module Bar; end  
> > &nbsp;&nbsp;&nbsp;&nbsp;=\> nil
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;[3] pry(main)\> x = Foo.new.extend(Bar)  
> > &nbsp;&nbsp;&nbsp;&nbsp;=\> #\<Foo:0x000000037df598\>
> > 
> > [...]
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;[7] pry(main)\> x.singleton\_class.ancestors  
> > &nbsp;&nbsp;&nbsp;&nbsp;=\> [#\<Class:#\<Foo:0x000000037df598\>\>, Bar, Foo, Object, PP::ObjectMixin, Kernel, BasicObject]
> 
> When Ruby tries to find a method, it walks up a \*\*single hierarchy\*\* of objects. It starts with the current object,

No. It starts with the singleton class. If you look at  
rb\_search\_method\_entry() you'll notice that obtaining the class is the  
first thing the method does:  
[https://github.com/ruby/ruby/blob/trunk/vm\_eval.c#L526](https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L526)

If you are interested how I got there:  
1. find -type f -name \*.c -exec egrep 'rb\_define\_method.\*send' {} +  
./vm\_eval.c:2205: rb\_define\_method(rb\_mKernel, "send", rb\_f\_send, -1);

2. rb\_f\_send() in vm\_eval.c  
[https://github.com/ruby/ruby/blob/trunk/vm\_eval.c#L979](https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L979)

3. rb\_send\_internal() in vm\_eval.c  
[https://github.com/ruby/ruby/blob/trunk/vm\_eval.c#L899](https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L899)

4. rb\_call0() in vm\_eval.c  
[https://github.com/ruby/ruby/blob/trunk/vm\_eval.c#L331](https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L331)

> then looks in the singleton class of the object, then the parents of that singleton class, then the parent class of the object, then the singleton class of that class and so on: one hierarchy involving the current object and any other number of classes, modules, singleton classes and (admittedly this would be pretty weird?) other objects.

The method lookup is more like this:  
1. Look in singleton class of instance  
2. Look in modules included in singleton class of instance in reverse  
inclusion order  
3. Look in super class of singleton class which happens to be the  
"official" class of the instance  
4. Look in modules included in class of instance in reverse inclusion order  
5. loop to step 3 with the super class of the current class looked at  
until method or end of class hierarchy found

Actually steps 1 and 3 and 2 and 4 are the same so you could condense  
this to three steps. Just listed for more clarity. You can see this  
order by doing obj.singleton\_class.ancestors - that gives you the  
order in which classes and modules are traversed.

With refinements things get more complicated.  
[http://ruby-doc.org//core-2.2.0/doc/syntax/refinements\_rdoc.html](http://ruby-doc.org//core-2.2.0/doc/syntax/refinements_rdoc.html)

> You can, of course, view things in terms of a separate class inheritance hierarchy if you wish. In other languages this is literally true. But really, Ruby isn't \_quite\_ working like that.

The fact that anything is an object in Ruby (which is good and allows  
to do quite some cool things, btw.) does not mean that mere objects  
which are not instances of Class or Method are part of the inheritance  
hierarchy. There is no generic object hierarchy that plays a role in  
method lookup. It's all classes and modules.

Cheers

robert

> **···**
>
> On Mon, Nov 14, 2016 at 4:14 PM, Andy Jones \<Andy.Jones@jameshall.co.uk\> wrote:
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
