I have a Date class which I would like to use to overwrite Ruby’s Date class. However, whenever I do a require 'Date' in my other files, it includes Ruby’s Date class and not my own.
I thought that putting it in a module would work well, so I did so within the Date.rb file:
module myModule
class Date
#...
end
end
However I still can’t figure out how to make my other classes include THIS Date class and not the built-in class. How can I achieve this?
All help is appreciated and thanks in advance!
Adam,
Your best bet is to simply follow some conventions:
date.rbnotDate.rb)libis a good candidate)my_date.rbor something) or if your class/module is name-spaced inside a module, put it in a folder of the module name (lib/my_module/date.rb).This removes any ambiguity in which file you are trying to load. If you absolutely must keep it named
date.rb, then load it with the full path by doing something like:File.join(File.dirname(__FILE__), "date.rb").For debugging purposes you can look at the following special variables to see what’s being loaded instead of your file:
$:will show the load path (i.e. every directory it looks in to find files to require. You will note that the current directory (.) is last. This is why your file isn’t loaded — it looks in the system path first. You can always move your current directory to the front of the load path as a solution by doing$:.unshift(File.dirname(__FILE__)), but I’d try one of the above approaches before resorting to this$"shows every file that has been required into your current environment so far.