Why does it fail? def Module1::Module2.hello

Hi, the following code fails:

module Module1
module Module2
end
end

def Module1::Module2.hello
puts “HELLO”
end

syntax error, unexpected keyword_end, expecting $end

But I can do:

def Module1.hello
puts “HELLO”
end

why does the former fail? Any trink?

Thanks a lot.

I know one way to define module method

module Module1::Module2
def self.hello
puts ‘HELLO’
end
end

On Fri, Aug 3, 2012 at 11:13 PM, Iñaki Baz C. [email protected]
wrote:

end

why does the former fail? Any trink?

Thanks a lot.


Iñaki Baz C.
[email protected]

William H.

[email protected]

2012/8/4 William H. [email protected]:

I know one way to define module method

module Module1::Module2
def self.hello
puts ‘HELLO’
end
end

Sure, but what I ask is why this works:

def Module1.hello ; end

end this fails:

def Module1::Module2.hello ; end

Because it’s how it defined in ruby syntax: def class; dot or color;
method name
You can’t use nesting in it. So it’s limited on a lexer level.

2012/8/4 Sigurd [email protected]:

Because it’s how it defined in ruby syntax: def class; dot or color; method
name
You can’t use nesting in it. So it’s limited on a lexer level.

Thanks for the clarification.

On Sat, Aug 4, 2012 at 7:07 PM, Iaki Baz C. [email protected] wrote:

[email protected]

As Sigurd has pointed out, it’s a syntax precedence thing. You can do
this however:

def (Module1::Module2).hello
puts “HELLO”
end

Regards,
Sean

2012/8/4 Sean O’Halpin [email protected]:

As Sigurd has pointed out, it’s a syntax precedence thing. You can do
this however:

def (Module1::Module2).hello
puts “HELLO”
end

Great! thanks a lot.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs