views:

207

answers:

6

I didn't take the usual CS route to learning programming and I often hear about namespaces but I don't really understand the concept. The descriptions I've found online are usually in the context of C which I'm not familiar with.

I am been doing Ruby for 2 years and I'm trying to get a deeper understanding of the language and OOP.

+3  A: 

just think of it as a logical grouping of objects and functionality

Cody C
+1  A: 

Wikipedia has a short but useful article on namespaces in general, as well as a much more detailed one for namespaces in computer science, with language-specific examples &c.

The key point is that, when you cannot "logically group" names into namespaces, i.e. you only have a single undifferentiated "spaces" where all names live, to avoid accidental clashes you end up clumsily re-implementing rudimental namespace functionality by such tricks as name prefixes &c. For example, "to draw" means something very different to an artist or to a gunslinger; if you couldn't have separate namespaces for artist-stuff and gunslinger-stuff, you'd end up with identifiers such as artist_draw and gunslinger_draw, and still risk accidental clashes if some library authors use different conventions, etc, etc.

Alex Martelli
+1  A: 

From php.net:

What are namespaces? In the broadest definition namespaces are a way of encapsulating items. This can be seen as an abstract concept in many places. For example, in any operating system directories serve to group related files, and act as a namespace for the files within them. As a concrete example, the file foo.txt can exist in both directory /home/greg and in /home/other, but two copies of foo.txt cannot co-exist in the same directory. In addition, to access the foo.txt file outside of the /home/greg directory, we must prepend the directory name to the file name using the directory separator to get /home/greg/foo.txt. This same principle extends to namespaces in the programming world.

So, as another poster mentioned, namespaces can be used to group items together.

To see why this might be useful, suppose you want to write a plug-in for, say, Wordpress, and you want to create a class named 'MyClass'. The trouble is, though, you have no idea if some other developer has already written another Wordpress plug-in using a class named 'MyClass'. So to avoid naming conflicts, instead you name your class 'MyPluginMyClass'. This is annoying, but it probably avoids naming conflicts.

But then comes the release of PHP 5.3, which finally supports namespaces (let's assume, too, that Wordpress and all of the servers on which it is deployed upgrade to PHP 5.3). Now you can create a namespace, say 'MyPlugin', and encapsulate 'MyClass' within it. Having done this, you can publish your plugin without worrying that your version of 'MyClass' will conflict with someone else's version of 'MyClass'.

Alex Basson
+5  A: 

Definition of namespace from wikipedia: A namespace is an abstract container or environment created to hold a logical grouping of unique identifiers (i.e., names). ...

Basicly, in the form of example and where you can find namespaces usable is something like this:

You define a constant or a variable, or even a class which has a generic name. Then, if dont wish to rename this constant/variable/class but need to have another one of with same name, you can define that new instance in different namespace.

In ruby, module is basicly the same thing as namespace is c++..

eg:

module foo
 baz = 1
end

module bar
 baz = 2
end

So, there, you have variable baz declared in two modules (aka namespaces in ruby)

rasjani
+3  A: 

A namespace provides a container to hold things like functions, classes and constants as a way to group them together logically and to help avoid conflicts with functions and classes with the same name that have been written by someone else.

In Ruby this is achieved using modules.

mikej
+4  A: 

I am going to provide a more commonplace description.

Say my wife has a sister named Sue, and so do I. How can I tell them apart in a conversation? By using their last name. The last name is the namespace.

This is a limited example of course, and in programming, the potential family members may be far more numerous than my example, therefore the potential for collisions is higher. Namespaces are also hierarchical in some languages (e.g. java), which is not paralleled with last names.

But otherwise it is a similar concept.

larson4