-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Time#floor and ceil #2201
Time#floor and ceil #2201
Conversation
@@ -228,6 +228,24 @@ def round(places = 0) | |||
time + (rounded - original) | |||
end | |||
|
|||
def floor(places = 0) | |||
original = to_i + subsec.to_r |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't Time#subsec already return a Rational and Rational#to_r is a no-op?
I see, there is a special case when subsec is zero:
> Time.new(0).subsec
=> 0
floored = original.floor(places) | ||
|
||
time = Time.allocate | ||
time.send(:initialize_copy, self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not simply time = dup
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, because the spec says so, alright:
it "returns an instance of Time, even if #floor is called on a subclass" do
subclass = Class.new(Time)
instance = subclass.at(0)
instance.class.should equal subclass
instance.floor.should be_an_instance_of(Time)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a common pattern isn't it? Maybe we should have some kind of Primitive.dup_as_class(x, T)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling initialize_copy
explicitly seems to be only used for Time so far.
Other classes probably build the new instance directly and pass all required state.
The issue with Time is there isn't a nice constructor to use, they are all very complicated.
Maybe we could have a Truffle::TimeOperations helper, but it seems a bit overkill.
On behalf of Maple, but not pinging her as she's on leave.
Shopify#1