Modules included in a class

Someone over at comp.lang.ruby asked for a way to find out which modules had been included in a class. I guessed that the ancestors method (defined in the Module class) would be the key, and that Array’s select would be the… other key… Huh. Anyway, this does it (assuming a class C): {|a| a.class == Module}

I even posted it as a solution. But what I should have looked closer, because there is exactly a method that does it, and it’s sort of obvious:


Of course, I don’t know the Module methods by heart, and I had just assumed that he had already looked for something in the documentation.

Moral of the story: Never assume a person asking a question has even so much as tried anything out on irb or looked at the documentation.

Bonus moral: Matz names everything what you would name it (pretty much). You can guess at Ruby methods and get it right an awful lot.


My .irbrc

I haven’t seen very many .irbrc files lying around on the internet. Examples, sure, I’ve seen examples, but nothing anyone would actually use. Maybe no one has taught me that things like .irbrc files are not interesting. I think they would be. I want to see more.

Here’s mine:


ARGV.concat [ "--readline" ]

require 'pp'
require 'rubygems'
require 'duration'
require 'wirble'
require 'irb/completion'
require 'irb/ext/save-history'

IRB.conf[:PROMPT][:SHORT] = {
  :PROMPT_C=>"%03n:%i* ",
  :PROMPT_I=>"%03n:%i> ",
  :PROMPT_N=>"%03n:%i> ",
  :PROMPT_S=>"%03n:%i%l "

IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"

Wirble.init(:skip_prompt => true, :skip_history => true)

at_exit { puts - IRB_START_TIME) }

def clear
  print "e[He[2J"

A couple of notes:

  1. Wirble colorizes return values in irb, and does a pretty good job at it. It also changes the prompt and the behavior of irb’s history in ways I don’t like; the Wirble.init line takes care of those problems.
  2. I was always trying to clear the screen with clear. I had to make it happen on my own. If I could suppress the nil that gets displayed afterwards, I would.
  3. The short prompt I make there is sort of a halfway between the standard prompt irb(main)001:0> and the simple prompt >>. I want line numbers and indent levels! I want to know where I am.
  4. I also want to know when I am. So I set IRB_START_TIME and use the Duration gem and at_exit to tell me how long my irb sessions are. It is totally worthless, but I love it. It makes me feel productive. Have a look:
mvb:~ cms$ irb
001:0> "wait a minute"
"wait a minute"
002:0> exit
20 seconds
mvb:~ cms$

I couldn’t wait a full minute.