How do I write a ruby method that may be called by appending the method name to an object?
i.e. get the quarter period of a specified date
def quarter(dateObject)
quarters = { 1 => 1, 2 => 1, 3 => 1, 4 => 2, 5 => 2, 6 => 2, 7 => 3, 8 => 3, 9 => 3, 10 => 4, 11 => 4, 12 => 4 }
quarters[dateObject.month]
end
I can use this method now like this:
quarter(Date.today)
but how do I manage to use it like this:
Date.today.quarter
or, even better, in both ways?
You can patch it into the class. Be wary of what you are doing and make sure the method doesn’t already exist when modifying a class.