I want to define an instance method Date#next
which returns the next day. So I made a DateExtension
module, like this:
module DateExtension
def next(symb=:day)
dt = DateTime.now
{:day => Date.new(dt.year, dt.month, dt.day + 1),
:week => Date.new(dt.year, dt.month, dt.day + 7),
:month => Date.new(dt.year, dt.month + 1, dt.day),
:year => Date.new(dt.year + 1, dt.month, dt.day)}[symb]
end
end
Using it:
class Date
include DateExtension
end
Calling the method d.next(:week)
makes Ruby throw an error ArgumentError: wrong number of arguments (1 for 0)
.
How can I override the default next
method from Date
class with the one declared in DateExtension
module?
question from:
https://stackoverflow.com/questions/5944278/overriding-method-by-another-defined-in-module 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…