The problem is that require
expects a String (e.g. require "path_to_module"
) but you're giving it a Class:
require User
require Report
If this is running in Rails, then Rails will autoload both of these classes. You don't need to do require
at all.
Edit: After you've removed those lines, Date_calc.months(6)
is still going to give you a NoMethodError, because months
is an instance method, but you're trying to call it as though it's a class method. You either need to call it on an instance of the Date_calc class, like this:
Date_calc.new.months(6)
Alternatively, you could define it as a class method by doing def self.months(range)
instead of def months(range)
, which would allow you to call Date_calc.months(6)
. But you might want to think about whether or not Date_calc should actually be a class, or if it should be a module.
It should be a class if you want to have multiple instances of it, like you would want an instance of a User or instance of a Report. "User" and "Report" are both nouns, because the class represents a thing that we might want to have more than one of. Does the sentence "I want to create a Date_calc" make sense? How about the sentence "I want to create two Date_calcs"? If you said yes to both, then a class makes sense.
If, however, you just want something to put some related methods in, but that doesn't represent a thing, then you might want to use a module instead. In Rails, for example, the Rails
object is not a class, it's a module. It doesn't make sense to say "I want to create a Rails," much less "I want to create two Rails." Rails is just a module used to group related methods together. If Date_calc fits that description, then it makes more sense as a module:
module Date_calc
def self.months(range)
# ...
end
end
# and then...
Date_calc.months(6)
As you can see, we use def self.months
instead of def months
, because we want to be able to call the method on the module itself.