about object methods

R

Ruby Newbee

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.
 
F

Florian Gilcher

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. =20=

The only difference is that classes have methods like #new and =20
#allocate. So, in your case: it doesn't really matter, but you could =20
check:

Dir.respond_to? :new

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

make so difference.

Regards,
Florian=
 
R

Robert Klemme

2009/12/14 Florian Gilcher said:
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:

=A0Dir.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
=3D> Class
irb(main):002:0> Enumerable.class
=3D> Module
irb(main):003:0>
For the methods themselves, there is no difference anyways: they belong t= o
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

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

Rajinder Yadav

Ruby said:
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


--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top