Posts Tagged ‘ruby’

Accessing Private Data in Ruby

Monday, January 29th, 2007

Of course that’s how you do it…


Every now and then you really do need to access a private variable and don’t particularly want to change the class. Especially in test code. Maybe it’s a smell, but I’d resorted to reopening classes and adding attr_accessor’s.

Moral issues aside, this is definitely a more elegant way of doing it.

Setting Today in a Test

Wednesday, December 20th, 2006

Ruby is awesome…

  def test_on_weekday
    today_is "May 3, 2006" do
      calendar.add("a call").at(
      assert_equal(Time.local(2006, 5, 6, 16),

…playing with code examples for our book.

Rubyforge Now Has Subversion!!!

Tuesday, January 24th, 2006

It’s about freekin’ time!

It looks like sourceforge isn’t far behind, either.

I’ve been torn on every new project I’ve started on either of these sites between self hosting, and getting tons of svn happiness, or being a good oss citizen and getting out of the box standards and integration. Now I don’t have to make that decision anymore.

Go team.

Autocompletion In Irb

Monday, October 24th, 2005

*Pro tip:


Seems like it’s on by default on windows. On other systems, you just need to
require 'irb/completion'
inside of irb to enable it. You can also start irb by typing
irb -r irb/completion
or you can add
require 'irb/completion'
to your .irbrc file To use it just hit tab – so typing
"foo".rev<tab>  -->  "foo".reverse
for example, or
to see all the methods you can call on foo

I was totally amazed that this was already there when charles lowell showed it to me

Debugging Ruby Seg Faults

Monday, June 14th, 2004

Ever been editing a ruby script that talks to a C library and the whole damn thing crashes saying “Illegal instruction” or “Bus error” or some other such nonhelpful message, with of course, no line number.

At this point, to figure out what the hell happened, you could break out your trusty “debug by printf’ing” strategy from your C days – OR - you could cheat.

What I do is put this line in my program at any point that I know for sure still executes :

    require 'Debugger'

and that’s it, next time I run my program I get a ton of messages like this :
turning on debugger
in ./chizzle/FileTreeView.rb:
in ./chizzle/Debugger.rb:
in ./chizzle/FileTreeView.rb:

the last line that my program was at before the seg fault is the culprit, simple as that.
——The Script——
What’s really cool is how simple this was to do in ruby. I think it took me about 10 minutes to write, and has since saved me countless hours. Let’s take a look at Debugger.rb :

    class Debugger
        def Debugger.turn_on
            puts 'turning on debugger'
            old_filename = ''
            set_trace_func(proc {|event_name, filename, line_number, object_id, binding, class_name|
                if filename != old_filename
                    puts "in #{filename}:"
                    old_filename = filename
                puts "\t#{event_name}:#{line_number}"

The Temp Files Pattern

Friday, December 19th, 2003

This is a pattern I first started using while working on rublog w/ Brian Marick et al. Dave Thomas gave it a better name when he checked it in, and I’ve renamed it since then. I think I like temp files. A second ago, I was working in nfit, and found myself writing it in c#. So I guess I like it.


You want to test something that accesses files. Something that you want to do all the time for some apps.


Create a class called !TempFiles, use it like this (for ruby):

def test_load
	files =
	files.add('wiki/captions/2003_11-November_captions.txt', CONTENTS)
	files.create do |dir|
		captions =, '/2003/11-November'), '/2003/11-November')
		assert_equal('Jilian', captions['apPle.JPG'])
		assert_equal('from the top', captions['bitter.jpg'])

It can be used from any test, and makes creating a file easy. From ruby the blocks make it really nice. I often add files in the setup code, but always have the block in the actual test. The nice thing about this is it’s easy to add files in your test right before creating them.

I’ll add the c# usage tomorrow

  • UPDATE *

this is now a gem called file_sandbox – sudo gem install file_sandbox – enjoy…