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
Method.new()
···
ruby -w test.rb
test1.rb:7: undefined method `new’ for Method:Class (NameError)
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?).
does ruby have such facilities?
thank you,
emmanuel
“Stop the world, I want to hop off!”
–Guy Bedos
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
···
On Friday 13 December 2002 09:51, Emmanuel Touzery wrote:
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
Method.new()
ruby -w test.rb
test1.rb:7: undefined method `new’ for Method:Class (NameError)
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?).
does ruby have such facilities?
thank you,
emmanuel
–
“Stop the world, I want to hop off!”
–Guy Bedos
Emmanuel Touzery emmanuel.touzery@wanadoo.fr writes:
namespace base {
include <base.h>
};
module BaseModule
class Foo
end
end
BaseModule.new ==> error
BaseModule::Foo.new ==> ok
include BaseModule
Foo.new ==> ok, create a new instance of BaseModule::Foo
class BaseClass
class Foo
end
end
BaseClass.new ==> ok
BaseClass::Foo.new ==> ok
include BaseClass ==> error
The most common way to declare a namespace is via module.
YS.
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.
thank you,
emmanuel
···
On Friday 13 December 2002 10:13, nobu.nokada@softhome.net wrote:
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.
–
“Stop the world, I want to hop off!”
–Guy Bedos