Jay McGavren's Journal

2008-06-02

Local aspects of modules?

Everyone’s so up in arms about re-opening core classes like Kernel and String in Ruby… Why not just apply changes to a target module only when viewed from within the current module or class?

class Bar
	def baz(fizz)
		fizz * 2
	end
end
module Foo
	local class Bar #Or some other keyword.
		def baz(fizz)
			fizz * 5
		end
	end
	def buzz
		puts Bar.new.baz('a') #'aaaaa'
	end
end
puts Bar.new.baz('a') #'aa'

That way you could tweak behavior to your heart’s content without worrying about the expectations of the other libraries you were using. This seems so obvious I’d be surprised if I was the first to think of it, so someone please let me know what problems have been encountered.

comments powered by Disqus