반사를 통해 Ruby 모듈 방법을 어떻게 호출합니까?
-
12-12-2019 - |
문제
다음을 수행하고 싶습니다.아이디어는 내가 경로에 메소드를 추가 할 수 있고 존재하는 경우 나중에 사용할 수 있습니다.Ruby 1.8을 사용하고 있습니다.
module Routes
def home
#stuff
end
def work
#end
end
end
Routes.method_defined? :home
# true
Routes.send(:home)
#NoMethodError: undefined method `home' for Routes:Module
. 해결책
다음 코드를 사용하면 다음을 수행 할 수 있습니다.
Class.new.extend(Routes).send(:home)
. 제휴하지 않습니다 StackOverflow