About object methods

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.

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

···

On 13 déc. 2009, at 23:46, Ruby Newbee <rubynewbee@gmail.com> 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?

Ruby Newbee 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/

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?

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 Yadav

http://DevMentor.org

Do Good! - Share Freely

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

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

···

2009/12/14 Florian Gilcher <flo@andersground.net>:

On 13 déc. 2009, at 23:46, Ruby Newbee <rubynewbee@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/