Irb session =

Ok, so I have to admit that I hate writing unit tests. I know that's
not orthodox. But it just seems like... work. I didn't get into this
for drudgery.

On the other hand, playing around with irb doesn't seem like work. It
seems like playing. Even tho I might end up testing the exact same
things. Irb is kind of like a polaroid camera: instant gratification.
I want to bring this level of instant interactivity to the process of
writing
automated tests.

So, is there a way to save a set of irb commands that I just typed to
a file, so they can be rerun later? Better yet, I'd like to save the
results of the commands too, and check to see if any results have
changed on a re-run.

So, is there a way to save a set of irb commands that I just typed to
a file, so they can be rerun later?

You can save the history of irb commands. See:

Better yet, I'd like to save the
results of the commands too, and check to see if any results have
changed on a re-run.

irb | tee log
If you're on *nix/cygwin.

HTH,
Assaph

"Caleb Clausen" <vikkous@gmail.com> asked:

So, is there a way to save a set of irb commands that I just typed to
a file, so they can be rerun later? Better yet, I'd like to save the
results of the commands too, and check to see if any results have
changed on a re-run.

I added the following to my ~/.irbrc

module IRB
  def IRB.history; @history; end
  @history = ""
  class WorkSpace
    alias old_evaluate evaluate
    def evaluate(context, statements, file = __FILE__, line = __LINE__)
      result = old_evaluate(context, statements, file, line)
      if /IRB\.history/.match(statements)
        IRB.history << "#{statements.chomp}\n"
      else
        IRB.history << "#{statements.chomp} #=> #{result.inspect}\n"
      end
      result
    end
  end
end

This will automatically collect your irb session history in the string
IRB.history. It might look like this:
    a = 1 + 2 #=> 3
    puts a #=> nil

One difference between this and the history thing on the wiki is that this
one saves the expected result in a comment after the line. Another is that
this will save a whole session of commands, whereas the wiki version is for
persistent history between sessions.

Cheers,
Dave