Monthly Archives: April 2010

OpenGem 1.4.2

I just gathered up a few patches and merged them into OpenGem. This includes:

  • Respect the VISUAL environment variable above EDITOR. (cwninja)
  • Use Launchy gem to read rdocs (ngauthier)

Install with gem install open_gem.

Leave a comment

Filed under ruby, Uncategorized

Easily Debug Ruby Loading Issues

Curious which files and gems are getting loaded and why?

def require path
  puts "require: (#{path}) #{caller[0]}"

def gem(*args)
  puts "gem: #{args.inspect} #{caller[0]}"

Save it off as require_inspector.rb, and then run it with ruby -r require_inspector.rb file_name.

1 Comment

Filed under ruby


grep and ack are indispensable tools, but they don’t know ruby. That’s where
RubyScope comes in. RubyScope lets you search your codebase for classes, methods, variables, and more. It understands ruby.

For instance you could use it to find all of your test methods:

ruby_scope -R --def '/^test/' ~/SecretProject

That would match:

def test_failing
  assert false

But it would skip this:

# TODO: test this later
def suspect_method
  test = 1 / 0


RubyScope is available as a normal gem:

gem install ruby_scope

Alternatively, you can get the source from GitHub:

git clone git://

If you pull the source, install with rake install.


Here are some other examples of things you can do with RubyScope.

Find all the places the variable name is assigned:

ruby_scope -R --assign 'name' .

Find any place that cake is mentioned in the code base, this could be a variable, a method call, a symbol, or any other bit of ruby code:

ruby_scope -R --any 'cake' .

I often want to know where a method was defined:

ruby_scope -R --def 'save_or_fail' .

If you’re not afraid of writing a SexpPath query you can even define custom queries:

ruby_scope --custom 's(:call, s(:ivar, atom), :save, _)'

That will find all of the places save is called on an instance variable, for instance.


Ruby is a great language, it’s very flexible, but sometimes that can be annoying. These are all the same:

a = 1 / 0.0
(a = 1/0.0)
a =
  1 / 0.0

Meanwhile it can be quite frustrating to be looking for where a got assigned this ridiculous value, and have to wade through all the different places a is used.

This also served as a first step towards common code completion and refactoring tools that could be used by any ruby editor.

Hacking RubyScope

Want to play with ruby_scope? Take a look at cli.rb, this is where all the queries are actually generated. Have an idea for a better caching mechanism? Look at sexp_cache.rb. Want to change how the hits are reported? Take a look at scanner.rb

The source is on GitHub, so go ahead and branch it:


Filed under ruby