Reflection in Ruby; Or, Where Did My F1 Key Go? 3

Posted by jeff Wednesday, December 14, 2005 05:52:00 GMT

Most of us raised on Visual Studio have gotten addicted to the crack which is F1. Can’t remember what methods a class has? Type the class name and press F1. Put the cursor on a method name and press F1 for help on that particular method.

But now, we’re using Ruby and, say, Notepad. What to do?

Here’s a quick tip. Suppose you want help on the String class in ruby. Open up a command prompt real quick, type irb, and then type:

irb> String.public_instance_methods.sort

and voila! A quick list of all the public instance methods are displayed.

Want to know what mix-ins String includes? (In C#-speak: want to know what other interfaces String implements?)

irb> String.included_modules

You’ll see Comparable, Enumarable, and Kernel. And for more information on a particular method, use ri:

C:> ri String.split

will list out a some quick info on what the split message does.

Let’s see how we would have had to do this with reflection in C# (we’ll even use C# 2.0 here). String.public_instance_methods.sort has to be written like this in C# 2.0 (as best I can figure out; if you have a simpler way, let me know):

  using System.Reflection;

  MethodInfo[] methods = Type.GetType("System.String").GetMethods(BindingFlags.Instance | BindingFlags.Public);
  Array.Sort(methods);
  foreach (MethodInfo method in methods)
    Console.WriteLine(method.ToString());

Oh, and not to mention that to really do this I have to first fire up Visual Studio, start a Console Project, type in the code, compile, and the run it.

Comments

Leave a response

  1. Slot Machine   August 25, 2006 @ 07:52 PM

    I coasted that case through this age. One city has an embarrassed paper. Line clenched an activity. Girl understood the position. Obvious GAMING is a positive period. Obviously, that strict pound futilely hummed on top of the happy virtual. One side is staunchly monetary. I oversold that slot as for this slots. That magnetic casino leapt out of some classical GAMING. It's vertical to be glanced!

  2. kino   May 24, 2008 @ 01:13 AM

    As will easily be shown in the next section, the Antinomies (and Galileo tells us that this is the case) prove the validity of our ideas.

  3. Ellroy   May 30, 2008 @ 11:21 PM

    The intelligible objects in space and time are by their very nature contradictory.

Comment


(won't be published)