Uninitialized constant Myclass (NameError)

Hi.

The following code worked in 1.6.8:

require ‘Mymodule’

include Mymodule

class Myclass
def quack2(q)
puts q
end
end

(Myclass was defined in Mymodule.rb within the module)

Now I get mc.rb:5: uninitialized constant Myclass (NameError)

If I change the line to: class Mymodule::Myclass
it works again.

I believe that using module::class notation is an option and the original way
should still work. Am I right?

Thanks

Dalibor Sramek

···


Dalibor Sramek http://www.insula.cz/dali | In the eyes of cats,
dalibor.sramek@insula.cz | all things belong to cats.

Hi.

The following code worked in 1.6.8:

require ‘Mymodule’

include Mymodule

This includes Mymodule in Object (and hence in every Ruby object), are
you sure you want to do that?

class Myclass
def quack2(q)
puts q
end
end

(Myclass was defined in Mymodule.rb within the module)

Now I get mc.rb:5: uninitialized constant Myclass (NameError)

If I change the line to: class Mymodule::Myclass
it works again.

I believe that using module::class notation is an option and the original way
should still work. Am I right?

I think what you really wanted was

class Mymodule
class Myclass

end
end

but nonetheless it’s good to know that this has changed.

···

On Mon, Aug 18, 2003 at 04:17:08PM +0900, Dalibor Sramek wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

‘Ooohh… “FreeBSD is faster over loopback, when compared to Linux
over the wire”. Film at 11.’
– Linus Torvalds

Now I get mc.rb:5: uninitialized constant Myclass (NameError)

[...]

I believe that using module::class notation is an option and the original way
should still work. Am I right?

There is an inconsistance between rb_const_defined_at() and
rb_const_get_at()

ruby can think that a constant is defined, but it will unable to retrieve
it.

Guy Decoux

Hi,

···

In message “Re: uninitialized constant Myclass (NameError)” on 03/08/18, ts decoux@moulon.inra.fr writes:

There is an inconsistance between rb_const_defined_at() and
rb_const_get_at()

ruby can think that a constant is defined, but it will unable to retrieve
it.

It must be a bug. I will fix when I finish the job at hand.

						matz.

Hi,

···

At Mon, 18 Aug 2003 18:23:45 +0900, ts wrote:

I believe that using module::class notation is an option and the original way
should still work. Am I right?

There is an inconsistance between rb_const_defined_at() and
rb_const_get_at()

Do you mean this?

Index: eval.c

RCS file: /src/ruby/eval.c,v
retrieving revision 1.515
diff -u -2 -p -r1.515 eval.c
— eval.c 14 Aug 2003 17:19:23 -0000 1.515
+++ eval.c 19 Aug 2003 17:00:37 -0000
@@ -3533,6 +3533,6 @@ rb_eval(self, n)
cbase = class_prefix(self, node->nd_cpath);
cname = node->nd_cpath->nd_mid;

  •   if (rb_const_defined_at(cbase, cname)) {
    
  •   klass = rb_const_get_at(cbase, cname);
    
  •   if (rb_const_defined(cbase, cname)) {
    
  •   klass = rb_const_get(cbase, cname);
      if (TYPE(klass) != T_CLASS) {
          rb_raise(rb_eTypeError, "%s is not a class",
    

@@ -3576,6 +3576,6 @@ rb_eval(self, n)
cbase = class_prefix(self, node->nd_cpath);
cname = node->nd_cpath->nd_mid;

  •   if (rb_const_defined_at(cbase, cname)) {
    
  •   module = rb_const_get_at(cbase, cname);
    
  •   if (rb_const_defined(cbase, cname)) {
    
  •   module = rb_const_get(cbase, cname);
      if (TYPE(module) != T_MODULE) {
          rb_raise(rb_eTypeError, "%s is not a module",
    


Nobu Nakada

Not really this (I've added comment in the source :-)))

int
rb_const_defined_at(klass, id)
    VALUE klass;
    ID id;
{
    VALUE value;

    if (RCLASS(klass)->iv_tbl && st_lookup(RCLASS(klass)->iv_tbl, id, &value)) {
  if (value == Qundef && NIL_P(autoload_file(klass, id)))
      return Qfalse;
  return Qtrue;
    }
   /*
      here rb_const_defined_at() can look in rb_cObject
   */
    if (klass == rb_cObject) {
  return rb_const_defined(klass, id);
    }
    return Qfalse;
}

VALUE
rb_const_get_at(klass, id)
    VALUE klass;
    ID id;
{
    VALUE value;

    while (RCLASS(klass)->iv_tbl && st_lookup(RCLASS(klass)->iv_tbl, id, &value)) {
  if (value == Qundef) {
      rb_autoload_load(klass, id);
      continue;
  }
  return value;
    }
    /*
       rb_const_get_at() don't do the same thing
       it give an error, rather than trying to look in rb_cObject
    */
    return const_missing(klass, id);
}

What is the right way to do ?

Guy Decoux

···

At Mon, 18 Aug 2003 18:23:45 +0900, > ts wrote:

There is an inconsistance between rb_const_defined_at() and
rb_const_get_at()

Do you mean this?

Hi,

Not really this (I’ve added comment in the source :-)))

Hmmm.

/*
here rb_const_defined_at() can look in rb_cObject
*/
if (klass == rb_cObject) {
return rb_const_defined(klass, id);
}

I guess this is surplus and rb_const_*_at should not search
other than given class.

What is the right way to do ?

rb_const_get and rb_const_defined could be integrated, and
it might be worthwhile.

The patch seems too big to post, so I put it at:
http://nokada.jin.gr.jp/ruby/const.diff.

···

At Wed, 20 Aug 2003 02:09:51 +0900, ts wrote:


Nobu Nakada

Hi,

···

In message “Re: uninitialized constant Myclass (NameError)” on 03/08/20, ts decoux@moulon.inra.fr writes:

Not really this (I’ve added comment in the source :-)))

I think I fixed the problem. See the latest CVS.

						matz.