메소드가 정의 된 클래스를 Ruby에서 어떻게 찾을 수 있습니까?Ruby에서 메소드가 어떤 클래스인지 어떻게 알 수 있습니까?
예를 들어 to_s
메서드를 구현하는 클래스를 알고 싶습니다. ri
명령을 사용하면 어떻게 할 수 있습니까?
메소드가 정의 된 클래스를 Ruby에서 어떻게 찾을 수 있습니까?Ruby에서 메소드가 어떤 클래스인지 어떻게 알 수 있습니까?
예를 들어 to_s
메서드를 구현하는 클래스를 알고 싶습니다. ri
명령을 사용하면 어떻게 할 수 있습니까?
~$ ri
Enter the method name you want to look up.
You can use tab to autocomplete.
Enter a blank line to exit.
>> to_s
= .to_s
(from gem actionpack-3.1.0.rc6)
=== Implementation from ActionDispatch::RemoteIp::RemoteIpGetter
------------------------------------------------------------------------------
to_s()
------------------------------------------------------------------------------
(from gem actionpack-3.1.0.rc6)
=== Implementation from ActionView::FileSystemResolver
------------------------------------------------------------------------------
to_s()
------------------------------------------------------------------------------
(from gem actionpack-3.1.0.rc6)
=== Implementation from ActionView::FixtureResolver
------------------------------------------------------------------------------
to_s()
편집 | 이 대답은 당신이 당신의 질문을 편집 한 이후로는 의미가 없습니다;) 맥락에서 벗어났습니다.
매우 효율적인 방법입니다. 당신이 필요할 것 왜 개인적으로 볼 수 없습니다 :
class ClassEnumerator
def each(&block)
ObjectSpace.each_object(Class, &block)
end
include Enumerable
end
ClassEnumerator.new.select { |klass| klass.instance_methods.include?(:merge) }
이것은 #merge
를 구현하는 모든 클래스를 찾아야한다.
pry(main)> ClassEnumerator.new.select { |klass| klass.instance_methods.include?(:merge) }
=> [OptionParser::CompletingHash,
OptionParser::OptionMap,
Hash,
Gem::Dependency,
Psych::Omap,
Psych::Set,
URI::MailTo,
URI::LDAPS,
URI::LDAP,
CodeRay::CaseIgnoringWordList,
CodeRay::WordList,
URI::HTTPS,
URI::HTTP,
URI::FTP,
URI::Generic]
pry(main)>
또한 ri METHOD_NAME
당신은 당신이 원하는 무엇의 예를 들어 줄 수 man ri
을 볼 입력 할 수 있습니다? – drnewman