Jay McGavren's Journal

2008-10-10

ActiveSupport, only when you need it...

I’m betting Rails enthusiasts would like to have access to ActiveSupport core extensions all the time, even when working in regular Ruby. But on my box, “require ‘activesupport’” incurs a 6-10 second delay, so I don’t really want to load it every time I bring up irb.

So I’m putting this in my Utility package:

module DeferredActiveSupport
	def method_missing(method, *arguments, &block)
		require 'activesupport'
		if self.respond_to?(method)
			self.send(method, *arguments, &block)
		else
			raise NoMethodError.new("#{method} not defined")
		end
	end
end

class String
	include DeferredActiveSupport
end

class Object
	include DeferredActiveSupport
end

#Other classes as desired...

Which lets me do this:

puts ({:foo => 'bar', :baz => 'glarch'}).to_json
puts 'foo'.titleize
puts 'test'.pluralize
puts 'large_pepperoni_pizza'.camelize

Yielding:

{"foo": "bar", "baz": "glarch"}
Foo
tests
LargePepperoniPizza
comments powered by Disqus