我被指派對現有的 Ruby gem 專案進行一些維護。我不是 Ruby 原生的,但有些檔案對我來說似乎是不必要的。
假設檔案夾a/b/c是專案的根目錄,代表模塊A::B::C。還有子模塊A::B::C::D1, A::B::C::D2,A::B::C::D3等。
我找不到邏輯的第一個檔案是檔案modules.rb夾中的檔案a/b/c。這包含整個專案中每個模塊的空模塊宣告:
module A::B::C
end
module A::B::C::D1
end
module A::B::C::D2
end
# etc
我試圖找出這個檔案的用途,但除了modules.rb實際包含所有代碼的示例(例如https://github.com/shrinidhi99/learn-ruby-for -fun/blob/master/Modules.rb , https://borg.garasilabs.org/andrew/ruby-training/-/blob/master/codes/modules.rb , https://zetcode.com/lang/rubytutorial /oop2/ )。在專案本身的任何地方也沒有提到它。它似乎實作的唯一一件事是在 ruby??doc.info 上提供檔案。這可能可以行內在單獨的檔案中。
除此之外,大多數模塊在檔案夾旁邊M都有一個匹配m.rb的m檔案。例如, file a/b/c.rb,a/b/c/d1.rb等。這個檔案除了require匹配模塊中所有檔案的陳述句之外什么都不包含。這意味著該檔案a/b/c.rb還具有等require陳述句。a/b/c/d1
這些 require 檔案中不包含其中的兩個子模塊A::B::C,這些模塊有自己的幾個子模塊。再加上每個檔案都應該只提到它自己的需求這一事實,表明這些需求檔案是完全沒有必要的。我能想到的唯一原因在這里提到:https ://stackoverflow.com/a/26470550/
我認為我可以簡單地洗掉這些檔案是錯誤的,還是它們仍然是必要的?還是這只是“Ruby 方式”?
uj5u.com熱心網友回復:
像這樣的東西應該作業。一個檔案——一個類(或模塊)。如果您有命名空間并且其中有一般邏輯 - 命名空間的單獨檔案
# a.rb
Dir[File.join(__dir__, 'a', '*.rb')].each { |f| require f }
module A
# general A logic here
end
# a/b.rb
Dir[File.join(__dir__, 'b', '*.rb')].each { |f| require f }
module A
module B
# general A::B logic here
end
end
# a/b/c.rb
module A
module B
module C
end
end
end
# a/b/d.rb
module A
module B
module D
end
end
end
# x.rb
require_relative 'a'
class X
include A::B::C
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460558.html
標籤:红宝石
下一篇:新的紅寶石。商品編號和數量
