Hello,
I have two questions,
- what’s the difference between class method and module method?
- 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.
Hello,
I have two questions,
Thanks.
On 13 déc. 2009, at 23:46, Ruby N. [email protected] wrote:
Hello,
I have two questions,
- what’s the difference between class method and module method?
- 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
2009/12/14 Florian G. [email protected]:
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
Ruby N. wrote:
Hello,
I have two questions,
- 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/
- 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)
…
…
[], chdir, chroot, delete, entries, foreach, getwd, glob, mkdir,
mktmpdir, new, open, pwd, rmdir, tmpdir, unlink
Thanks.
–
Kind Regards,
Rajinder Y.
Do Good! - Share Freely
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs