Using modules as namespaces to override built-in classes in Ruby -


I have a date class that I use to overwrite Ruby's However, whenever I need in my other files, it still contains the Ruby's date class and I do not own it.

I thought it would work well in a module, so I did this in the Date.rb file:

  module myModule Class date # ... and end  

However, I still can not understand how it is included in my other classes, the date class and not the underlying class . How can I get this?

All help is appreciated and thanks in advance!

Adam,

Your best bet is to just follow some conventions: / P>

  1. Always have your file names lower case ( date.rb not date .rb )
  2. inside your library Keep your files in a specific directory ( lib is a good candidate)
  3. Call your files in Ruby classes (this is called my_date.rb or something Please) or if your class If the module / has a namespace inside a module, place it in the folder named module ( lib / my_module / date.rb ).

This removes any ambiguity in which you are trying to load the file. If you should name it by date.rb , then load it with full path: File.join (File.dirname (__ FILE__), "date.rb") .

  • $:

    The code will show the load path (i.e. in each directory it is necessary that you can find the files. You note that the current directory (. ) is the last, this is the reason your file is not loaded - - It appears in the system path before. You always have your current instructions You can take the front of the load path, such as $:. Unshift (File.dirname (__ file __)), but I

  • $ " / Code> shows each file that has become necessary in your current environment.

Comments