# About object methods

**URL:** <https://www.ruby-forum.com/t/about-object-methods/180177>\
**Category:** Ruby\
**Created:** [December 14, 2009, 4:17am UTC](https://www.ruby-forum.com/t/about-object-methods/180177 "2009-12-14T04:17:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 4:17am UTC](https://www.ruby-forum.com/t/about-object-methods/180177/1 "2009-12-14T04:17:38Z")

</div>

Hello,

I have two questions,

1. what’s the difference between class method and module method?
2. when I say: Dir.pwd, is this a class method or a module method? How  
to know if Dir is a module or a class?

Thanks.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 5:34am UTC](https://www.ruby-forum.com/t/about-object-methods/180177/2 "2009-12-14T05:34:54Z")

</div>

On 13 dÃ©c. 2009, at 23:46, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I have two questions,
> 
> 1. what’s the difference between class method and module method?
> 2. when I say: Dir.pwd, is this a class method or a module method? How  
> to know if Dir is a module or a class?

If you check Class.ancestors, you will find that classes are modules.  
The only difference is that classes have methods like #new and  
#allocate. So, in your case: it doesn’t really matter, but you could  
check:

Dir.respond\_to? :new

For the methods themselves, there is no difference anyways: they  
belong to objects. Whether that is a class, a module or something else  
make so difference.

Regards,  
Florian

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 10:18am UTC](https://www.ruby-forum.com/t/about-object-methods/180177/3 "2009-12-14T10:18:44Z")

</div>

2009/12/14 Florian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> only difference is that classes have methods like #new and #allocate. So, in  
> your case: it doesn’t really matter, but you could check:
> 
> Dir.respond\_to? :new

There is a more direct variant which is also safer (you can make a  
module respond to :new):

irb(main):001:0\> Dir.class  
=\> Class  
irb(main):002:0\> Enumerable.class  
=\> Module  
irb(main):003:0\>

> For the methods themselves, there is no difference anyways: they belong to  
> objects. Whether that is a class, a module or something else make so  
> difference.

That’s true - technically. There is still a semantic difference:  
class objects are always singletons while instances are typically  
many. This mainly determines what functionality goes into a class  
method and what into an instance method.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 10:20am UTC](https://www.ruby-forum.com/t/about-object-methods/180177/4 "2009-12-14T10:20:29Z")

</div>

Ruby N. wrote:

> Hello,
> 
> I have two questions,
> 
> 1. what’s the difference between class method and module method?

a class method well belongs to the class =P  
there can only be one class method, you don’t need to create an  
object(instance) to call a class method

a module method is a way to package methods using namespacing,  
a module is not a like a class, you can create instance of a class but  
not a module

more on modules:  
[http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/)

> 1. when I say: Dir.pwd, is this a class method or a module method? How  
> to know if Dir is a module or a class?

use ri to display class information, it clearly states ‘pwd’ is a class  
method,  
type ‘ri Dir’ at the prompt,  
type ‘ri’ alone to find out how to use it

yadav@KubuntuX64:$ ri Dir

------------------------------------------------------------- Class: Dir  
Objects of class +Dir+ are directory streams representing  
directories in the underlying file system. They provide a variety  
of ways to list directories and their contents. See also +File+.

…  
…  
(edit)  
…  
…

## Class methods:

```
  [], chdir, chroot, delete, entries, foreach, getwd, glob, mkdir,
  mktmpdir, new, open, pwd, rmdir, tmpdir, unlink

```

> Thanks.

–  
Kind Regards,  
Rajinder Y.

[http://DevMentor.org](http://DevMentor.org)

Do Good! - Share Freely
