the second one, but only if the parent modules actually exist before you start:
your original:
jonathan@ADAMS-3DJ5PW2:~$ irb
2.5.8 :001 > module Project
2.5.8 :002?> module Alert
2.5.8 :003?> class NotifyService
2.5.8 :004?> end
2.5.8 :005?> end
2.5.8 :006?> end
=> nil
2.5.8 :007 > Project.new
Traceback (most recent call last):
2: from /home/jonathan/.rvm/rubies/ruby-2.5.8/bin/irb:11:in `<main>'
1: from (irb):7
NoMethodError (undefined method `new' for Project:Module)
2.5.8 :008 > Project::Alert.new
Traceback (most recent call last):
2: from /home/jonathan/.rvm/rubies/ruby-2.5.8/bin/irb:11:in `<main>'
1: from (irb):8
NoMethodError (undefined method `new' for Project::Alert:Module)
2.5.8 :009 > Project::Alert::NotifyService.new
=> #<Project::Alert::NotifyService:0x000055a483abd808>
2.5.8 :010 > exit
your second (without definition):
jonathan@ADAMS-3DJ5PW2:~$ irb
2.5.8 :001 > class Project::Alert::NotifyService
2.5.8 :002?> end
Traceback (most recent call last):
2: from /home/jonathan/.rvm/rubies/ruby-2.5.8/bin/irb:11:in `<main>'
1: from (irb):1
NameError (uninitialized constant Project)
Did you mean? Object
2.5.8 :003 > exit
your second, with the addition of module definitions first:
jonathan@ADAMS-3DJ5PW2:~$ irb
2.5.8 :001 > module Project
2.5.8 :002?> module Alert
2.5.8 :003?> end
2.5.8 :004?> end
=> nil
2.5.8 :005 > class Project::Alert::NotifyService
2.5.8 :006?> end
=> nil
2.5.8 :007 > Project.new
Traceback (most recent call last):
2: from /home/jonathan/.rvm/rubies/ruby-2.5.8/bin/irb:11:in `<main>'
1: from (irb):7
NoMethodError (undefined method `new' for Project:Module)
2.5.8 :008 > Project::Alert.new
Traceback (most recent call last):
2: from /home/jonathan/.rvm/rubies/ruby-2.5.8/bin/irb:11:in `<main>'
1: from (irb):8
NoMethodError (undefined method `new' for Project::Alert:Module)
2.5.8 :009 > Project::Alert::NotifyService.new
=> #<Project::Alert::NotifyService:0x00005604a90e79b0>
2.5.8 :010 > exit
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…