Where's A?

T

Trans

This doesn't seem right to me:

irb(main):001:0> module X
irb(main):002:1> module A; end
irb(main):003:1> end
=> nil
irb(main):004:0> class Q
irb(main):005:1> include X
irb(main):006:1> p A
irb(main):007:1> class Y
irb(main):008:2> class Z < A
irb(main):009:3> end
irb(main):010:2> end
irb(main):011:1> end
X::A
NameError: uninitialized constant Q::Y::A
from (irb):8

If I recall correctly, 1.9 has some changes to constant lookup rules.
Is the above fixed in 1.9?

Thanks,
T.
 
S

Sean O'Halpin

This doesn't seem right to me:

irb(main):001:0> module X
irb(main):002:1> module A; end
irb(main):003:1> end
=> nil
irb(main):004:0> class Q
irb(main):005:1> include X
irb(main):006:1> p A
irb(main):007:1> class Y
irb(main):008:2> class Z < A
irb(main):009:3> end
irb(main):010:2> end
irb(main):011:1> end
X::A
NameError: uninitialized constant Q::Y::A
from (irb):8

If I recall correctly, 1.9 has some changes to constant lookup rules.
Is the above fixed in 1.9?

Thanks,
T.
Hi Trans,

I'm not sure I understand what you expect. In the following example:

class A
end

module Outer
module X
class A # you meant 'class' didn't you? ;)
end
end

class A
end

class Q
include X
class Y
p A
class Z < A # which A do you want?
end
# this seems reasonable to me...
class XZ < X::A
end
end
end
end

which A should Z be a subclass of?

Regards,
Sean
 
T

Trans

Hi Trans,

I'm not sure I understand what you expect. In the following example:

class A
end

module Outer
module X
class A # you meant 'class' didn't you? ;)

Oops. Yes.
end
end

class A
end

class Q
include X
class Y
p A
class Z < A # which A do you want?
end
# this seems reasonable to me...
class XZ < X::A
end
end
end
end

which A should Z be a subclass of?

Fair question. I would expect the closest, and in this case it would
be the one defined in X, since that's what is asked for in the
namespace with "include X". The problem with the way it is now is very
much like the problem with XML namespaces. That X::, which seems so
reasonable here turns into Some::Deep::Space:: in real life. And if
we're defining a number of such subclasses (as I am) we have to repeat
it over and over. No fun.

However, I did find a trick.

module X
class A
include X
end
end

This helps once I get past the first subclass (all subsequent
subclasses can see all of X). Weird, huh?

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top