Module methods in nested modules with 1.8.0

Hi,

I haven’t found anything in the archives about this issue:
How can I define a module method in a nested module definition?

I can write:
module A
module B
def B.hello()
end
end
end

Which works fine.
But I can’t write this:

module A
end
module A::B
def B.hello()
end
end

Because Ruby tells me:
uninitialized constant A::b::B (NameError)

So, how do I define my hello now?

Thanks in advance,
Padre

module A::B
  def B.hello()

     def self.hello

  end
end

Guy Decoux

I predict this will work:

module A; end

module A::B
def A::B.hello()
end
end

···

On Wednesday, August 13, 2003, 9:40:06 PM, Papp wrote:

But I can’t write this:

module A
end
module A::B
def B.hello()
end
end

Because Ruby tells me:
uninitialized constant A::b::B (NameError)

So, how do I define my hello now?

Of course I’ve tried it, too. But it’s a syntax error.

Padre

···

On Wed, 13 Aug 2003, Gavin Sinclair wrote:

I predict this will work:

module A; end

module A::B
def A::B.hello()
end
end

Hi,

···

At Wed, 13 Aug 2003 21:02:23 +0900, Papp Zoltan wrote:

I predict this will work:

module A; end

module A::B
def A::B.hello()
end
end

Of course I’ve tried it, too. But it’s a syntax error.

def (A::B).hello()
end

Singleton receiver must be a variable or a parenthesized
arbitrary expression.


Nobu Nakada

Hmmm, that’s a bit odd.

irb(main):005:0> def (“x”).foo
irb(main):006:1> end
SyntaxError: compile error
(irb):5: can’t define single method for literals
def (“x”).foo
^
from (irb):6
irb(main):007:0> def (“x” + “y”).foo
irb(main):008:1> end
=> nil

Not so “arbitrary” after all.

Gavin

···

On Thursday, August 14, 2003, 4:39:06 PM, nobu wrote:

Hi,

At Wed, 13 Aug 2003 21:02:23 +0900, > Papp Zoltan wrote:

I predict this will work:

module A; end

module A::B
def A::B.hello()
end
end

Of course I’ve tried it, too. But it’s a syntax error.

def (A::B).hello()
end

Singleton receiver must be a variable or a parenthesized
arbitrary expression.

No. But how do you call the defined singleton method, considering the
fact string literals return independent string objects each time they
evaluated. The error is much better than the singleton method
vanished away.

						matz.
···

In message “Re: Module methods in nested modules with 1.8.0” on 03/08/14, Gavin Sinclair gsinclair@soyabean.com.au writes:

irb(main):005:0> def (“x”).foo
irb(main):006:1> end
SyntaxError: compile error
(irb):5: can’t define single method for literals

Not so “arbitrary” after all.

I quite agree, but

def ([4] + [5]).foo
end

works. What you wrote above applies here as well.

Not that I care, mind you.

Gavin

···

On Thursday, August 14, 2003, 6:35:28 PM, Yukihiro wrote:

In message “Re: Module methods in nested modules with 1.8.0” > on 03/08/14, Gavin Sinclair gsinclair@soyabean.com.au writes:

irb(main):005:0>> def (“x”).foo
irb(main):006:1>> end

SyntaxError: compile error
(irb):5: can’t define single method for literals

Not so “arbitrary” after all.

No. But how do you call the defined singleton method, considering the
fact string literals return independent string objects each time they
evaluated. The error is much better than the singleton method
vanished away.

Hi,

···

In message “Re: Module methods in nested modules with 1.8.0” on 03/08/14, Gavin Sinclair gsinclair@soyabean.com.au writes:

def ([4] + [5]).foo
end

works. What you wrote above applies here as well.

Indeed. The literal case is reported since it is very apparent.

						matz.