# Another Ruby language question

**URL:** <https://www.ruby-forum.com/t/another-ruby-language-question/83584>\
**Category:** Ruby\
**Created:** [January 27, 2007, 6:16am UTC](https://www.ruby-forum.com/t/another-ruby-language-question/83584 "2007-01-27T06:16:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [January 27, 2007, 6:16am UTC](https://www.ruby-forum.com/t/another-ruby-language-question/83584/1 "2007-01-27T06:16:08Z")

</div>

Following from yesterday I have a few more Ruby questions.

Can someone please explain what ‘self’ does in the following:

module Mod1  
def self.a\_method  
puts ‘a’  
end

def b\_method  
puts ‘b’  
end  
end

If self isn’t specificed I’m unable to call the method. ex.  
Mod1::b\_method fails.

I’ve scoured on-line docs about module and come up empty.

Next. What does ‘/upload’ mean in:  
class Upload \< R ‘/upload’  
This is from:

> **[O'Reilly Media - Technology and Business Training](https://www.oreilly.com)**
>
> Gain technology and business knowledge and hone your skills with learning resources created and curated by O'Reilly's experts: live online training, video, books, our platform has content from 200+ of the world's best publishers.

Thanks.

* * *

Neville F., [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [January 27, 2007, 6:43am UTC](https://www.ruby-forum.com/t/another-ruby-language-question/83584/2 "2007-01-27T06:43:53Z")

</div>

On 1/27/07, Neville F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> puts 'b'
> 
> ```
> 
> end  
> end
> 
> If self isn’t specificed I’m unable to call the method. ex.  
> Mod1::b\_method fails.

So in this particular case, ‘self’ here refers to the Module called  
Mod1. Your code defines a method (a\_method) on the module itself which  
allows you to call it with the :: syntax.

The other method (b\_method) is merely a method defined inside the  
module. In this case the module is acting as a namespace. If you  
‘include’ this module somewhere in your code you’ll be adding the  
b\_method to that place (scope?)

(disclaimer: I’m still learning ruby myself, so that may not be the  
best answer ever, but it is how I understand it… And knowing these  
guys these days I’ll be corrected if I’m wrong, mere moments from  
now.)

> Next. What does ‘/upload’ mean in:  
> class Upload \< R ‘/upload’  
> This is from:  
> [O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/ruby/blog/2007/01/the_joy_of_rolling_your_own_wi.html)

Right here you’re fishing in \_why territory (specifically the camping  
framework) where things are way over my head, someone else will have  
to help you there.

hth,  
-Harold

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [January 27, 2007, 7:07am UTC](https://www.ruby-forum.com/t/another-ruby-language-question/83584/3 "2007-01-27T07:07:21Z")

</div>

Harold H. wrote:

> On 1/27/07, Neville F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ```
> > puts 'b'
> > 
> > ```
> > 
> > end  
> > end
> > 
> > If self isn’t specificed I’m unable to call the method. ex.  
> > Mod1::b\_method fails.
> 
> So in this particular case, ‘self’ here refers to the Module called  
> Mod1. Your code defines a method (a\_method) on the module itself which  
> allows you to call it with the :: syntax.
> 
> The other method (b\_method) is merely a method defined inside the  
> module. In this case the module is acting as a namespace. If you  
> ‘include’ this module somewhere in your code you’ll be adding the  
> b\_method to that place (scope?)
> 
> (disclaimer: I’m still learning ruby myself, so that may not be the  
> best answer ever, but it is how I understand it… And knowing these  
> guys these days I’ll be corrected if I’m wrong, mere moments from  
> now.)

Harold,  
Thanks for that. I figured I understood the use of self, but assumed it  
was redundant and therefore I couldn’t see the difference between  
a\_method and b\_method. Now I get it. In my C++ world we don’t qualify  
methods like this.

* * *

Neville F., [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [January 27, 2007, 7:25am UTC](https://www.ruby-forum.com/t/another-ruby-language-question/83584/4 "2007-01-27T07:25:55Z")

</div>

On 1/27/07, Neville F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> was redundant and therefore I couldn’t see the difference between  
> a\_method and b\_method. Now I get it. In my C++ world we don’t qualify  
> methods like this.

No problemo.

In comparison to C++, your a\_method is (kinda sorta) like a static  
method, but not really, but kindof because you can use the :: syntax  
to call it.

b\_method is more like a method belonging to a class which you’re  
inheriting. The instances of your new class can call b\_method  
(assuming you’ve included Mod1) …

Ruby only supports single inheritance directly, but you can emulate  
multiple inheritance like this by just including multiple modules into  
your class… I believe the rubyists like to call this mixing-in…

hth,  
-Harold

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [January 27, 2007, 7:27am UTC](https://www.ruby-forum.com/t/another-ruby-language-question/83584/5 "2007-01-27T07:27:16Z")

</div>

So I assume that:

module Mod1  
def self.a\_method  
puts ‘a’  
end  
end

is identical to:

module Mod1  
def Mod1.a\_method  
puts ‘a’  
end  
end

* * *

Neville F., [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [January 27, 2007, 7:32am UTC](https://www.ruby-forum.com/t/another-ruby-language-question/83584/6 "2007-01-27T07:32:56Z")

</div>

On 1/27/07, Neville F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> module Mod1  
> def Mod1.a\_method  
> puts ‘a’  
> end  
> end

indeed.

(apologies to anyone not reading this in a threaded fashion for the  
extremely short reply.)

-Harold

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [January 27, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/another-ruby-language-question/83584/7 "2007-01-27T22:00:29Z")

</div>

On 1/26/07, Neville F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> puts 'b'
> 
> ```
> 
> end  
> end
> 
> If self isn’t specificed I’m unable to call the method. ex.  
> Mod1::b\_method fails.
> 
> I’ve scoured on-line docs about module and come up empty.

I see people already helped you with this one

> Next. What does ‘/upload’ mean in:  
> class Upload \< R ‘/upload’

This is the beginning of a controller definition in the Camping  
microframework. The class is “inheriting” from the method R() which  
takes a regex argument of the route to bind to. The R() method then  
adds some meta-info to the runtime, and returns an anonymous class  
that Upload actually inherits from.
