Getting to know Ruby: Core Classes

This evening I've been hanging out in #ruby-lang helping people out and just chatting about some more advanced Ruby topics. I decided to jump on the console and play around with some Ruby internals. If you're a programmer and are curious about Ruby but aren't sure where to start, this is the perfect article for you.

First and foremost, you'll need to have Ruby installed on your machine. Doing this is pretty straightforward, click here for help. Once you have it installed, you'll get a nice little utility called IRB which stands for Interactive Ruby. To start it up, open a console window and type:


$ irb

You'll be presented with the signature ">>" command prompt. To make sure everything is working, go ahead and do a "Hello, World" like so:


>> puts "Hello, World!"

If you get the following output, you're ready to go:


Hello, World!
=> nil

A good resource to bookmark is the RDoc Documentation for Core Classes. Now is a good time to open this up in a browser. Once that is open, go back to IRB and enter the following line of code:


>> Object.constants.select { |c| Object.const_get(c).is_a?(Class) }.sort

Let's break that line down a bit. Since Object is the root namespace/class in Ruby, the constants class method will return all the modules and classes defined within Object. Since that returns an array of constants (it is actually an array of strings for each constant), we'll want to slim down the list to only those that are classes. The const_get class method returns the constant and the is_a? class method tells whether or not a given constant descends from a given class. Then we sort that list. Returned should be the 65 or so core classes that make up the Ruby language, see below:


=> ["ArgumentError", "Array", "Bignum", "Binding", "Class", "ConditionVariable", "Continuation", "Data", "Dir", 
"EOFError", "Exception", "FalseClass", "File", "Fixnum", "Float", "FloatDomainError", "Hash", "IO", "IOError", 
"IndexError", "Integer", "Interrupt", "LoadError", "LocalJumpError", "MatchData", "MatchingData", "Method", "Module", 
"Mutex", "NameError", "NilClass", "NoMemoryError", "NoMethodError", "NotImplementedError", "Numeric", "Object", 
"Proc", "Queue", "Range", "RangeError", "Regexp", "RegexpError", "RubyLex", "RuntimeError", "SLex", "ScriptError", 
"SecurityError", "SignalException", "SizedQueue", "StandardError", "String", "Struct", "Symbol", "SyntaxError", 
"SystemCallError", "SystemExit", "SystemStackError", "Thread", "ThreadError", "ThreadGroup", "Time", "TrueClass", 
"TypeError", "UnboundMethod", "ZeroDivisionError"]

If you really want to get to know Ruby internals, just go through that list and refer to the documentation I mentioned before. Since that is simply a list of alphabetized class names, some classes will be far more complex than others. Feel free to skip those that are unclear.

Also, if you want the same documentation from the website locally, you can use the RI utility as follows (for the Array documentation):


$ ri Array

Have fun!

Reader Comments

Cheri A

Hi Jordan,

Nice article! Could you please suggest a good way to find out the time it takes to process an sql request in rails (i.e. to retrieve data from the database)?

Jordan Fowler

Hi Cheri,

I think you're looking for a way to benchmark your database calls. Take a look at: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001411

--Jordan

Your Voice: