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

Posted by "Iñaki Baz Castillo" <ibc@aliax.net> (Guest)
on 2012-08-03 17:14
(Received via mailing list)
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.
Posted by William Herry (Guest)
on 2012-08-04 04:09
(Received via mailing list)
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 Castillo <ibc@aliax.net> 
wrote:

> end
>
> why does the former fail? Any trink?
>
> Thanks a lot.
>
> --
> Iñaki Baz Castillo
> <ibc@aliax.net>
>
>


--



William Herry
====================
WilliamHerryChina@Gmail.com
Posted by "Iñaki Baz Castillo" <ibc@aliax.net> (Guest)
on 2012-08-04 20:10
(Received via mailing list)
2012/8/4 William Herry <william.herry.china@gmail.com>:
> 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
Posted by Alex Newone (sigurd_p)
on 2012-08-04 20:33
(Received via mailing list)
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.
Posted by "Iñaki Baz Castillo" <ibc@aliax.net> (Guest)
on 2012-08-04 20:46
(Received via mailing list)
2012/8/4 Sigurd <cu9ypd@gmail.com>:
> 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.
Posted by Sean O'halpin (sean)
on 2012-08-04 22:06
(Received via mailing list)
On Sat, Aug 4, 2012 at 7:07 PM, Iaki Baz Castillo <ibc@aliax.net> wrote:
>
> <ibc@aliax.net>
>

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
Posted by "Iñaki Baz Castillo" <ibc@aliax.net> (Guest)
on 2012-08-04 22:19
(Received via mailing list)
2012/8/4 Sean O'Halpin <sean.ohalpin@gmail.com>:
> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.