# Lookup up documentation for inherited methods

**URL:** <https://www.ruby-forum.com/t/lookup-up-documentation-for-inherited-methods/183629>\
**Category:** Ruby\
**Created:** [February 17, 2010, 5:40pm UTC](https://www.ruby-forum.com/t/lookup-up-documentation-for-inherited-methods/183629 "2010-02-17T17:40:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [February 17, 2010, 5:40pm UTC](https://www.ruby-forum.com/t/lookup-up-documentation-for-inherited-methods/183629/1 "2010-02-17T17:40:45Z")

</div>

I find it frustrating when using “ri” to look up documentation on a  
class, only to find the method I’m curious about is not listed.

As an example: String objects have many more methods than are explained  
in the documentation for String (example: String.grep). When you run  
“ri” on String.grep, you get “Nothing known about String.grep”. This is  
apparently because the grep method was inherited from another class.

Is there any way to have ri automatically check the documentation of  
parent classes if no documentation is found for a method of a particular  
object? Or is there a better way to access Ruby’s documentation than “ri  
Class.method”? I don’t want to have to do detective work to look up a  
simple method…

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [February 17, 2010, 6:07pm UTC](https://www.ruby-forum.com/t/lookup-up-documentation-for-inherited-methods/183629/2 "2010-02-17T18:07:40Z")

</div>

Nick B. wrote:

> I find it frustrating when using “ri” to look up documentation on a  
> class, only to find the method I’m curious about is not listed.
> 
> As an example: String objects have many more methods than are explained  
> in the documentation for String (example: String.grep). When you run  
> “ri” on String.grep, you get “Nothing known about String.grep”. This is  
> apparently because the grep method was inherited from another class.
> 
> Is there any way to have ri automatically check the documentation of  
> parent classes if no documentation is found for a method of a particular  
> object? Or is there a better way to access Ruby’s documentation than “ri  
> Class.method”? I don’t want to have to do detective work to look up a  
> simple method…

I usually find rdoc (as available at [railsapi.com](http://railsapi.com) or [ruby-doc.org](http://ruby-doc.org)) to be  
more usable than ri.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [February 17, 2010, 6:12pm UTC](https://www.ruby-forum.com/t/lookup-up-documentation-for-inherited-methods/183629/3 "2010-02-17T18:12:57Z")

</div>

Nick B. wrote:

> I find it frustrating when using “ri” to look up documentation on a  
> class, only to find the method I’m curious about is not listed.
> 
> As an example: String objects have many more methods than are explained  
> in the documentation for String (example: String.grep). When you run  
> “ri” on String.grep, you get “Nothing known about String.grep”. This is  
> apparently because the grep method was inherited from another class.
> 
> Is there any way to have ri automatically check the documentation of  
> parent classes if no documentation is found for a method of a particular  
> object? Or is there a better way to access Ruby’s documentation than “ri  
> Class.method”? I don’t want to have to do detective work to look up a  
> simple method…

## Maybe you can’t find the grep method in String, because it hasn’t such a method?

## ruby -v: ruby 1.9.1p378 (2010-01-10 revision 26273) [x86\_64-linux] irb(main):001:0\> “ABC”.grep NoMethodError: undefined method `grep' for "ABC":String from (irb):1 from /home/marvin/Programmieren/Programme/ruby_shell/ruby_shell.rb:52:in`’ irb(main):002:0\>

## And no, I don’t know of a way to check inherited methods. But you could do

ri grep  
More than one method matched your request. You can refine your  
search by asking for information on one of:

## Enumerable#grep [Ruby 1.9.1] Rake::FileList#egrep [gem rake-0.8.7]

So, #grep is defined in Enumerable. Then you can look for  
Enumerable#grep.

Marvin

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [February 17, 2010, 6:52pm UTC](https://www.ruby-forum.com/t/lookup-up-documentation-for-inherited-methods/183629/4 "2010-02-17T18:52:00Z")

</div>

On 02/17/2010 06:12 PM, Marvin Gülker wrote:

> > parent classes if no documentation is found for a method of a particular  
> > from (irb):1  
> > More than one method matched your request. You can refine your  
> > search by asking for information on one of:
> 
> ## Enumerable#grep [Ruby 1.9.1] Rake::FileList#egrep [gem rake-0.8.7]
> 
> So, #grep is defined in Enumerable. Then you can look for  
> Enumerable#grep.
> 
> Marvin

When I lookup Array#grep I get the documentation of Enumerable#grep it  
seems:

robert@fussel:~$ ri19 -T Array#grep  
-------------------------------------------------------- Enumerable#grep  
enum.grep(pattern) =\> array  
enum.grep(pattern) {| obj | block } =\> array

```
  From Ruby 1.9.1

```

* * *

```
  Returns an array of every element in _enum_ for which +Pattern ===
  element+. If the optional _block_ is supplied, each matching
  element is passed to it, and the block's result is stored in the
  output array.

     (1..100).grep 38..44 #=> [38, 39, 40, 41, 42, 43, 44]
     c = IO.constants
     c.grep(/SEEK/) #=> [:SEEK_SET, :SEEK_CUR, :SEEK_END]
     res = c.grep(/SEEK/) {|v| IO.const_get(v) }
     res #=> [0, 1, 2]

```

So, at least in 1.9, it seems that what Nick asks for does happen  
already. Am I missing something?

When looking for the class defining a method, using #instance\_method  
helps:

robert@fussel:~$ ruby19 -e ‘p Array.instance\_method :grep’  
#\<UnboundMethod: Array(Enumerable)#grep\>  
robert@fussel:~$ ruby19 -v  
ruby 1.9.1p376 (2009-12-07 revision 26041) [i686-linux]

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [February 17, 2010, 8:04pm UTC](https://www.ruby-forum.com/t/lookup-up-documentation-for-inherited-methods/183629/5 "2010-02-17T20:04:04Z")

</div>

> Maybe you can’t find the grep method in String, because it hasn’t such a  
> method?

Ruby 1.8.7 has that method.

$ ruby -v  
ruby 1.8.7 (2009-06-12 patchlevel 174) [x86\_64-linux]  
$ ruby -e ‘puts String.new.respond\_to?(:grep)’  
true

> ri grep  
> More than one method matched your request. You can refine your  
> search by asking for information on one of:

This will help. It’s not as convenient as automatically checking the  
docs for parent classes, but it is at least an easier way of figuring  
out where inherited methods come from.

> So, at least in 1.9, it seems that what Nick asks for does happen

Interesting. Yeah, I’m working with 1.8.7. I guess that’s just one more  
reason to upgrade to 1.9! Now if only I can convince my hosting  
providers to do so…

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [February 17, 2010, 6:36pm UTC](https://www.ruby-forum.com/t/lookup-up-documentation-for-inherited-methods/183629/6 "2010-02-17T18:36:45Z")

</div>

> Is there any way to have ri automatically check the documentation of  
> parent classes if no documentation is found for a method of a particular  
> object? Or is there a better way to access Ruby’s documentation than “ri  
> Class.method”? I don’t want to have to do detective work to look up a  
> simple method…

Hmm. There is a little utility I use within irb (for me it’s easier  
than rdoc or ri)  
$ gem install ri\_for

It doesn’t appear helpful in this case (except for telling us that  
String might well not have that method).

It does, however, lookup ancestors appropriately if that were the  
problem, and run ri against the “right” ancestor.

> > require ‘ri\_for’  
> > =\> true
> 
> > ‘’.ri\_for :grep  
> > NameError: undefined method `grep' for class`String’  
> > from  
> > E:/installs/ruby191p376/lib/ruby/gems/1.9.1/gems/ri\_for-0.5.1/lib/ri\_for/method\_ri.rb:180:in  
> > `method’  
> > from (irb):2
> 
> > String.ri\_for :grep  
> > NameError: appears that this object String does not have this method  
> > grep  
> > from  
> > E:/installs/ruby191p376/lib/ruby/gems/1.9.1/gems/ri\_for-0.5.1/lib/ri\_for/method\_ri.rb:170:in  
> > `ri_for' from (irb):3 from E:/installs/ruby191p376/bin/irb:12:in`’

The way ri\_for determines the “real” ancestor is to do a .method(:name)  
call on it, like

> > ‘’.method(:select)  
> > =\> #\<Method: String(Kernel)#select\>

within the #inspect output it tells me the real ancestor is Kernel.  
GL!  
-rp

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [February 17, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/lookup-up-documentation-for-inherited-methods/183629/7 "2010-02-17T22:00:24Z")

</div>

Nick B. wrote:

> I find it frustrating when using “ri” to look up documentation on a  
> class, only to find the method I’m curious about is not listed.
> 
> As an example: String objects have many more methods than are explained  
> in the documentation for String (example: String.grep). When you run  
> “ri” on String.grep, you get “Nothing known about String.grep”. This is  
> apparently because the grep method was inherited from another class.

So use FastRI instead of RI. It knows to look in the ancestors.

Or do just “ri grep” and it will list the objects where that method is  
defined.
