Skip to main content
Engineering LibreTexts

2.12: Finding Methods

  • Page ID
    39739
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Sometimes you can guess the name of a method, or at least part of the name of a method, more easily than the name of a class. For example, if you are interested in the current time, you might expect that there would be a method called ”now”, or containing ”now” as a substring. But where might it be? Spotter and Finder can help you.

    Spotter

    With Spotter you can also find methods. Either by getting a class and navigating or using category such as:

    • #implementor a method name will display all the methods that are implemented and have the same name. For example you will get all the do: methods.
    • #selector a method name will display all the selectors that matches this name

    With Finder

    Select World Menu > Tools > Finder. Type now in the top left search box, click Search (or just press the Enter key). You should see a list of results similar to the one in Figure \(\PageIndex{1}\).

    Finder showing all classes defining a method named now.
    Figure \(\PageIndex{1}\): The Finder showing all classes defining a method named now.

    The Finder will display a list of all the method names that contain the substring ”now”. To scroll to now itself, move the cursor to the list and type ”n”; this type-ahead trick works in all scrolling windows. Expanding the ”now” item shows you the classes that implement a method with this name. Selecting any one of them will display the source code for the implementation in the code pane on the bottom.

    Finding methods using examples

    At other times, you may have a good idea that a method exists, but will have no idea what it might be called. The Finder can still help! For example, suppose that you would like to find a method that turns a string into upper case (for example, transforming 'eureka' into 'EUREKA'). We can give the inputs and expected output of a method and the Finder will try to find it for you.

    The Finder has a really powerful functionality: you can give the receiver, arguments and expected result and the finder tries to find the corresponding message.

    Trying Finder

    In the Finder, select the Examples mode using the second combo-box (the one that shows Selectors by default).

    Type 'eureka' . 'EUREKA' into the search box and press the Enter key.

    The Finder will then suggest a method that does what you were looking for, as well as display a list of classes that implement methods with the same name. In this case, it determined that the asUppercase method is the one that performed the operation that fit your example.

    Click on the 'eureka' asUppercase --> 'EUREKA' expression, to show the list of classes that implement that method.

    An asterisk at the beginning of a line in the list of classes indicates that this method is the one that was actually used to obtain the requested result. So, the asterisk in front of String lets us know that the method asUppercase defined in the class String was executed and returned the result we wanted. The classes that do not have an asterisk are just other implementors of asUppercase, which share the method name but were not used to return the wanted result. So the method Character>>asUppercase was not executed in our example, because 'eureka' is not a Character instance (but is instead a String).

    You can also use the Finder to search for methods by arguments and results. For example, if you are looking for a method that will find the greatest common factor of two integers, you might try 25 . 35 . 5 as an example. You can also give the method finder multiple examples to narrow the search space; the help text in the bottom pane explains how.


    This page titled 2.12: Finding Methods is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.