ruby namespaces 
Author Message
 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

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>

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  
 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

> 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


Tue, 31 May 2005 16:53:17 GMT  
 ruby namespaces
Hi,

At Fri, 13 Dec 2002 17:46:48 +0900,

Quote:

> -----------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)
> --------------------------------

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  
 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.

thank you,

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



Tue, 31 May 2005 17:17:35 GMT  
 ruby namespaces

Quote:

> 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.



Tue, 31 May 2005 17:39:49 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Bruce Eckel's opinion of Ruby [SmallScript/AOS Selector Namespaces]

2. Ruby/Mock namespace question

3. namespace partitioning in Ruby

4. Namespaces in Scheme vs. Namespaces in CL.

5. To namespace or not to namespace

6. Problem with using a Namespace variable in another namespace function

7. Importing a Namespace Failing to Import Namespace??

8. Namespace of a namespace question.

9. Namespaces in Scheme vs. Namespaces in CL.

10. BUG/PROBLEM: Tcl8.0b1 namespace children/delete {} in a namespace

11. namespaces and objects (namespace ::<classname>)

12. pthread trouble with ruby-opengl on FreeBSD (was: Re: [announcement] Ruby 3D Ruby)

 

 
Powered by phpBB® Forum Software