ruby namespaces

ruby namespaces

Author

Message

Emmanuel Touzer#1 / 5

ruby namespaces

Hello,

I looked in the pickaxe book for details about namespaces, there is mention of modules, but though i'm not sure, I think it's not what I want. Look at this: -----------8<----------------- class Method def initialize() puts "ok" end end

If I replace "Method" by "MyMethod", i get as an output "ok". So I conclude that ruby already provides a class named "Method". I would like to still be able to have my own class named "Method", either by wrapping the ruby method in its own namespace, which i would do in C++ say like this: namespace base { # include <base.h>

Quote:

};

either by putting my own class in a namespace (is it enough? if i'm one "namespace", will my class be preffered to those in the root namespace?).

does ruby have such facilities?

thank you,

emmanuel -- "Stop the world, I want to hop off!" --Guy Bedos

Tue, 31 May 2005 16:46:48 GMT

Emmanuel Touzer#2 / 5

ruby namespaces

argl..

module Test class Method def initialize() puts "ok" end end

Method.new() end

seems to fix it. it didn't seem to work in my bigger real program though. i'll check..

sorry for the noise.

emmanuel

Quote:

> Hello,

> I looked in the pickaxe book for details about namespaces, there is > mention of modules, but though i'm not sure, I think it's not what I want. > Look at this: > -----------8<----------------- > class Method > def initialize() > puts "ok" > end > end

> If I replace "Method" by "MyMethod", i get as an output "ok". So I conclude > that ruby already provides a class named "Method". I would like to still be > able to have my own class named "Method", either by wrapping the ruby > method in its own namespace, which i would do in C++ say like this: > namespace base { > # include <base.h> > };

> either by putting my own class in a namespace (is it enough? if i'm one > "namespace", will my class be preffered to those in the root namespace?).

Method is a builtin class made by Kernel#method and Module#instance_method, and has no `new' class method. You shouldn't overwrite its initialize method.

-- Nobu Nakada

Tue, 31 May 2005 17:13:51 GMT

Emmanuel Touzer#4 / 5

ruby namespaces

Quote:

> Method is a builtin class made by Kernel#method and > Module#instance_method, and has no `new' class method. You > shouldn't overwrite its initialize method.

yes, now my class is still named Method, but I wrapped it in a Module, and i never include this module; i only type ModuleName::Method so i guess it's ok (at least it does work). I just didn't want to name it OOModule or such, it's really dumb if the language has namespaces.