Search code examples
rubyclassconflict

My class name conflicting with Ruby's


I have a class in my module that is called "Date". But when i want to utilize the Date class packaged with ruby, it uses my Date class instead.

module Mymod
  class ClassA
    class Date < Mymod::ClassA
      require 'date'

      def initialize
        today = Date.today # get today's date from Ruby's Date class
        puts "Today's date is #{today.to_s}"
      end
    end
  end
end

Mymod::ClassA::Date.new

The ouput from running this is

test.rb:7:in `initialize': undefined method `today' for Mymod::ClassA::Date:Class (NoMethodError)

Is there any way I can reference ruby's Date class from within my own class also called "Date"?


Solution

  • def initialize
            today = ::Date.today # get today's date from Ruby's Date class
            puts "Today's date is #{today.to_s}"
          end
    

    What is double colon in Ruby