0

I would like to have a clear function in my irb console, but there isn't one. Here's what I type in every time I load irb from the terminal:

def cls
  system 'clear'
end

It's not real hard to type this each time a load irb, but it would certainly be nice to have this function load automatically when irb starts.

Is it possible to do this?

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
dingalingchickenwiing
  • 1,937
  • 3
  • 20
  • 30

2 Answers2

1

When irb is starting up, it looks for the file .irbrc in your home directory. If the file exists, it evaluates it. So this file is the perfect place to add some generic stuff to irb...

For inspiration, mine looks like this:

require 'rubygems'
require 'pp'
require 'irb/ext/save-history'

# add $HOME/lib to the load path
$: << '~/lib'

IRB.conf[:AUTO_INDENT] = true
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"


def real_instance_methods_of(klass)
  klass.instance_methods - ((klass.ancestors - [klass]).map(&:instance_methods).flatten)
end

class Class
  def instance_methods_defined_here
    real_instance_methods_of self
  end
end

# more stuff...
# ...

EDIT: I just noticed the comment by Dave Newton now; he already pointed out the .irbrc solution...

severin
  • 10,148
  • 1
  • 39
  • 40
-2

on a mac just hit CMD + K to clear the screen

Stuart Nelson
  • 4,202
  • 2
  • 23
  • 26
  • 2
    This is a very narrow answer. Where does he mention anything about Mac? – Adam Eberlin Oct 04 '12 at 03:10
  • 1
    ...except for all those Ruby users on Linux or Windows. – willglynn Oct 04 '12 at 03:21
  • 2
    it's a joke, have a little fun. and a downvote? if he's on a mac it's a valid solution. – Stuart Nelson Oct 04 '12 at 03:22
  • 3
    How does Ruby imply a Mac? Meh; of course it's a valid solution if you're in a Mac. Of course, if you're on a Mac, ctrl-l would work just as well, without deleting your scroll buffer. In general we prefer asking a question if you have one instead of guessing, though. – Dave Newton Oct 04 '12 at 03:48
  • @StuartNelson: Acceptance of humor on SO is whimsical :), but not absent, see [this question](http://stackoverflow.com/questions/1732348). – Boris Stitnicky Oct 04 '12 at 07:50