On 02/17/2010 06:12 PM, Marvin G=FClker wrote:
> Nick Brown 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 explaine=
d
>> in the documentation for String (example: String.grep). When you run
>> "ri" on String.grep, you get "Nothing known about String.grep". This i=
s
>> 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 particul=
ar
>> 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...
>=20
> Maybe you can't find the grep method in String, because it hasn't such =
a=20
> 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=20
> /home/marvin/Programmieren/Programme/ruby_shell/ruby_shell.rb:52:in=20
> `<main>'
> irb(main):002:0>
> ---------------------------------------
>=20
> And no, I don't know of a way to check inherited methods. But you could=
=20
> do
> ---------------------------------------
> ri grep
>      More than one method matched your request.  You can refine your
>      search by asking for information on one of:
>=20
> 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=20
> Enumerable#grep.
>=20
> Marvin

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

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

      From Ruby 1.9.1
------------------------------------------------------------------------
      Returns an array of every element in _enum_ for which +Pattern =3D=3D=
=3D
      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   #=3D> [38, 39, 40, 41, 42, 43, 44]
         c =3D IO.constants
         c.grep(/SEEK/)         #=3D> [:SEEK_SET, :SEEK_CUR, :SEEK_END]
         res =3D c.grep(/SEEK/) {|v| IO.const_get(v) }
         res                    #=3D> [0, 1, 2]


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


When looking for the class defining a method, using #instance_method help=
s:

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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/