MyRubyClass.reload!
August 8th, 2008 by Ben Wagaman.Categorized as programming, Ruby on Rails.
Today I was writing some specs with RSpec, the Ruby BDD Testing Framework. In between tests, classes aren’t reloaded by default, so I went about try to figure out how to reload a class for certain tests.
Active Support defines a method on Class called remove_class. This provides half of the equation. The other half was to reload the class again. So, I wrote a method on the Object class that should handle most cases. Note, this may not work with namespaced classes, but it did the quick and dirty job of helping my specs to reload the class.
# Call this method like
# MyRubyClass.reload!
# or
# MyRubyClass.reload!('/path/to/file/my_ruby_class.rb')
class Object
def reload!(file = nil)
remove_class(self)
load(file || "#{self.to_s.underscore}.rb")
end
end
All I needed to do was then call this method when I needed to reload the class.
MyRubyClass.reload!
RSS Feed
We started writing BDD-style tests on our .NET project… one of the guys on the team has done some Ruby and he’s a big RSpec fan. It’s not quite as easy as it easy in Ruby but it’s still really cool.
Let me guess, is it Steve Harman? He just started working with us over at Chase. By the way, I’ll see you up at CodeMash.