irb session =

C

Caleb Clausen

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.
 
D

Dave Burt

Caleb Clausen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top