0
패치 net/http를 시도하고 하나의 서비스 클래스에만 적용해야합니다. 세련미가가는 길 같습니다. 원숭이 패치는 작동하지만 세련미는 없습니다. 이것은 네임 스페이스 문제입니까? 이 프로젝트는 루비 2.3.0을 사용하고 있지만 2.4.1로 시도했지만 원숭이 패치 만 적용됩니다. 원숭이 패치와상세 검색 및 네임 스페이스
:
module Net
class HTTPGenericRequest
def write_header(sock, ver, path)
puts "monkey patched!"
# patch stuff...
end
end
end
Service.new.make_request
# monkey patched!
정제로 :
이module NetHttpPatch
refine Net::HTTPGenericRequest do
def write_header(sock, ver, path)
puts "refined!"
# patch stuff...
end
end
end
class Service
using NetHttpPatch
end
Service.new.make_request
# :(
업데이트 :
이 현명 유사한 범위를 것 같다? 분명히 net/http가 요청을하면 범위가 줄어들고 더 복잡한 일이 일어나고 있습니까?
module TimeExtension
refine Fixnum do
def hours
self * 60
end
end
end
class Service
using TimeExtension
def one_hour
puts 1.hours
end
end
puts Service.new.one_hour
# 60
업데이트 UPDATE :
NVM, 내가 :) 지금 무슨 일이 일어나고 있는지 볼이나 mixin이 어떻게 작동하는지와 using
혼합에서 당신의 두뇌를 유지해야합니다.
module TimeExtension
refine Fixnum do
def hours
self * 60
end
end
end
class Foo
def one_hour
puts 1.hours
end
end
class Service
using TimeExtension
def one_hour
puts 1.hours
end
def another_hour
Foo.new.one_hour
end
end
puts Service.new.one_hour
# 60
puts Service.new.another_hour
# undefined method `hours' for 1:Fixnum (NoMethodError)
새로운 인스턴스에서 호출 될 때 구체화를 추가하는보다 간단한 예제로 질문을 업데이트했습니다. 뭔가 빠졌을 수 있습니다. :) – kreek