Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

How to convert multi-line module/class into single line with double colons(::) in Ruby?

This is an example of a class I have:

module Project
  module Alert
    class NotifyService
    end
  end
end

Now I want to replace that with colons(::).

Option 1:

module Project::Alert::NotifyService
end

Option 2:

class Project::Alert::NotifyService
end

I don't want these to happen:

# NotWanted A
class Project
  class Alert
    class NotifyService
    end
  end
end

# or
# NotWanted B

module Project
  module Alert
    module NotifyService
    end
  end
end

Which is the equivalent of the original one? And is there a way to tell that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...