module M2
include M
class TestM
def setup; C.blah; end
end
end
Won't work because the C tries to refer to TestM::C, which doesn't
exist.
Previously, when you did an "include M" you included the module into
Kernel.
Since:
TestM is an instance of Class
Class is an instance of Object
You've included M in Object, causing Object::C to be created
So, in that case TestM::C exists.
···
-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of List Recv
Sent: Wednesday, 21 December 2005 4:14 PM
To: ruby-talk ML
Subject: Help with object scope
I'm having a really tough time with Ruby's scope (modules, include,
etc.).
I had a file, in short:
module M
class C
end
end
# Unit Tests
include M
class TestM
def setup; C.blah; end
end
Works great.
*However*, if I wrap the unit tests in their own module, then
ruby can
no longer find class C. Why? Didn't I include module M? I'm really
confused here.
If you want the full source, I can post it.
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################
#####################################################################################
This e-mail message has been scanned for Viruses and Content and cleared
by NetIQ MailMarshal
#####################################################################################
So,
1) How do I call C from class TestM without explicitly using its
namespace (M:C)? Is there no way? I don't want to modify Kernel or
anything, just have class C available.
2) Could you explain exactly what a Module is? A namespace? A set of
methods without a home? I'm really confused... (I know that
module_define and class_define are synonomous, which is even more
confusing...)
Daniel Sheppard wrote:
···
module M
class C
def self.blah
end
end
end
module M2
include M
class TestM
def setup; C.blah; end
end
end
Won't work because the C tries to refer to TestM::C, which doesn't
exist.
Previously, when you did an "include M" you included the module into
Kernel.
Since:
TestM is an instance of Class
Class is an instance of Object
You've included M in Object, causing Object::C to be created
So, in that case TestM::C exists.
I had a file, in short:
end
Works great.
*However*, if I wrap the unit tests in their own module, then
ruby can
no longer find class C. Why? Didn't I include module M? I'm really
confused here.
If you want the full source, I can post it.
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################
#####################################################################################
This e-mail message has been scanned for Viruses and Content and cleared
by NetIQ MailMarshal
#####################################################################################
2) Could you explain exactly what a Module is? A namespace? A set of methods without a home? I'm really confused... (I know that module_define and class_define are synonomous, which is even more confusing...)
So,
1) How do I call C from class TestM without explicitly using its namespace (M:C)? Is there no way? I don't want to modify Kernel or anything, just have class C available.
Try this instead:
class TestM
include M
def setup; C.blah; end
end
A module is a set of instance methods and constants. It can either be used as a namespace or mix-in functionality. (For the latter you are collecting similar methods / constants together in one Module so you can add them to your own classes or objects easily.)
mod_or_cls.include(mod) will add all constants and instance methods of mod to mod_or_cls.
obj.extend(mod) will add all constants and instance methods of mod to an object itself.
Here's a sample:
module HalfString
def half()
self[0, length / 2]
end
end