This project is archived and is in readonly mode.
ActiveSupport::Duration#* should return a Duration, not an Integer
-
Levin Alexander
however, this does not make "2 * 1.day" return a duration. The only way to do that would be to overwrite Numeric#* and test for the type of the argument.
I don't think the benefits would justify the performance hit of doing that.
-
Dan Barry
Actually, this does make 2 * 1.day return a duration. I don't know exactly why it works, but there's a test for it (test_multiplication_associativity).
-
Levin Alexander
no, it does not. This test fails:
def test_multiplication_associativity assert_equal "2.months", (1.month * 2).inspect assert_equal "2.months", (2 * 1.month).inspect endit just looks like it works, because durations are converted to seconds, before they are compared.
-
Levin Alexander
there is a small error in the test. It has to say "2 months" (without dot) instead of "2.months"
-
Dan Barry
Ah, good catch. I'll have to update the tests and figure that out.
-
Levin Alexander
Attached is just the Duration#* part of your (Dan's) patch, because that part seems uncontroversial.
This patch does not change behavior of Numeric*Duration and it does not implement Duration#/
It also does not check if the multiplier is a Fixnum because it is not clear what the fallback should be if it isn't (could call "to_i", "round", raise an argument error)
-
Pratik
- Assigned user set to Geoff Buesing
- Tag changed from active_support, duration to active_support, duration, patch
-
Geoff Buesing
- State changed from new to wontfix
Unsure about this one. Reasons:
-
With this change, 1.day * 2 returns a Duration, but 2 * 1.day returns a Fixnum. Seems odd and unexpected.
-
We disallow creating a Duration via Float#months and #years (e.g., you can no longer do "5.3.years"), but this patch would allow you to mutliply by a Float (e.g. "1.year * 5.3")
