我剛剛開始Ruby並學習模塊的概念。我明白模塊的一個用途是更好地組織代碼並避免名稱衝突。比方說,我有一堆這樣的模塊(我還沒有列入執行情況這並不重要) :Ruby模塊和類
module Dropbox
class Base
def initialize(a_user)
end
end
class Event < Base
def newFile?
end
def newImage?
end
end
class Action < Base
def saveFile(params)
end
end
end
和另一個模塊:
module CustomURL
class Base
def initialize(a_user, a_url, a_method, some_args, a_regex)
end
end
class Event < Base
def initialize(a_user, a_url, a_method, some_args, a_regex)
end
def change?
end
end
class Action < Base
def send_request(params)
end
end
end
我將有一堆這些模塊(10+,用於Gmail,Hotmail等)。我試圖弄清楚的是,這是組織我的代碼的正確方法嗎?
基本上,我使用的模塊來表示「服務」,所有服務都將有一個共同的接口類(基地初始化,行動的操作列表和事件監控)。
我建議將其移至codereview.stackexchange.com。你的代碼起作用了,它並不像它可能的那樣嚴密。 – 2012-01-29 16:57:29