다음 질문으로 나의 질문을 요약합니다. 메서드 범위를 제어하는 방법은 무엇입니까?
# /somewhere/build_ability/car.rb
module BuildAbility
module Car
def build_car
build_component
end
def build_component
# Specific designed for build car, not common used
# Totally different from build_component in BuildAbility::Ship
end
end
end
# /somewhere/build_ability/ship.rb
module BuildAbility
module Ship
def build_ship
build_component
end
def build_component
# Specific designed for build ship, not common used
# Totally different from build_component in BuildAbility::Car
end
end
end
# /somewhere/omni_factory.rb
class OmniFactory
include BuildAbility::Car
include BuildAbility::Ship
def build_component
# common used method
end
end
의 목적
는OmniFactory
에
BuildAbility::Car
이
build_car
을 소개하고
build_component
을 소개하고자하지 않는 것입니다 포함되어 있습니다.
build_component
(BuildAbility::Car
)은 build_car
으로 설계되었으며, 다른 곳에서는 사용하지 않습니다. build_car
. 따라서 OmniFactory
에 BuildAbility::Car
이 포함 된 경우 build_component
을 소개 할 필요가 없습니다.
같은 상황이 BuildAbility::Ship
입니다.
car.rb
에 넣어야합니까?
나는 보석에서 이런 일이 일어날지도 모른다고 생각했는데, 보석은 사용자에게 노출 된 몇 가지 저수준 기본 기능을 원하지 않을 수 있으며, 보석 파일에서만 사용되었다.
다음 코드는 클로저와 같은 솔루션 중 하나입니다. 그러나이 방법은 너무 추해 보입니다. 다른 해결책이 있습니까?
# /somewhere/build_ability/car.rb
module BuildAbility
module Car
build_component = lambda {
# Specific designed for build car, not common used
# Totally different from build_component in BuildAbility::Ship
}
define_method :build_car do
build_component.call
end
end
end
# /somewhere/build_ability/ship.rb
module BuildAbility
module Ship
build_component = lambda {
# Specific designed for build ship, not common used
# Totally different from build_component in BuildAbility::Car
}
define_method :build_ship do
build_component.call
end
end
end
# /somewhere/omni_factory.rb
class OmniFactory
include BuildAbility::Car
include BuildAbility::Ship
def build_component
# common used method
end
end