Ruby Newbee wrote: > Coz Ruby's class and module are close to Perl's, I think it's not hard > to understand for them. > I wrote a note below for learning them, if I'm not correct, please > point it out, thanks. Worth also pointing out: * class Class inherits from class Module * a class *is* a module * the extra functionality that a class has, over and above a module, is that you can create an instance of a class. > 2. If we want to call the methods in a class, need to instantiate the > class with "new" (except the class method). > If we want to call the methods in a module, need to include it > (except the module method). In addition: Objects have singleton classes, which are private to that object. You can use 'extend' to add a module to an individual object's singleton class. module Foo def greet puts "Hello, #{self}!" end end s = "you" s.extend Foo s.greet # => "Hello, you!" Note we have not touched class String at this point. But since classes are objects too, the same applies to classes. You can use a module to add "class methods" to a class. String.extend Foo String.greet # => "Hello, String!" > 3. class itself has class method, module itself has module method. There are also 'module functions', defined like this: module Foo def bar puts "hello" end module_function :bar end Foo.bar (There is a subtle difference between this and def Foo.bar, see if you can work it out...) > 4. the calling and called relationship between class and module: > > A module can contain methods, constants, other modules, and even > classes. What do you mean by "contain" here? irb(main):026:0> module X irb(main):027:1> end => nil irb(main):028:0> class Y irb(main):029:1> include X irb(main):030:1> end => Y irb(main):031:0> module Z irb(main):032:1> include Y irb(main):033:1> end TypeError: wrong argument type Class (expected Module) from (irb):32:in `include' from (irb):32 from :0 5. modules (and hence also classes) form a namespace mechanism, which is entirely orthogonal to inheritance and the method call chain. module X class Y Z = 3 end end puts X::Y::Z y = X::Y.new This is maybe what you were alluding to in point 4, because you mentioned constants. But it must be stated this is *nothing* to do with inheritance at all. -- Posted via http://www.ruby-forum.com/.