[Feature] Numeric#milliseconds to complement #minutes, #hours, #days, etc.

26 views
Skip to first unread message

Tim Rogers

unread,
Apr 18, 2018, 12:09:48 PM4/18/18
to Ruby on Rails: Core
ActiveSupport monkeypatches `Numeric` to add time-related functionality (see activesupport/lib/active_support/core_ext/numeric/time.rb), giving you some nice sugar for working with time, e.g.:

1.second
2.hours
3.days
4.months
5.years

At the moment, there's an `#in_milliseconds` method which converts a number of seconds to a number of milliseconds (e.g. `5.in_milliseconds == 5000`).

However, there isn't a `#milliseconds` monkeypatch. I can imagine this being useful for cases like `sleep(5.milliseconds)` or `expect(racing_car.last_repaired_at).to be_within(5.milliseconds).of(Time.zone.now)`.

I'd be willing to build this; it should be a small change.

What do people think?

Jeremy Daer

unread,
Apr 18, 2018, 1:10:20 PM4/18/18
to rubyonra...@googlegroups.com
I’d use this. 👍
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Uysim Ty

unread,
Oct 5, 2018, 12:42:29 AM10/5/18
to Ruby on Rails: Core
I support this

Greg Navis

unread,
Oct 5, 2018, 7:00:04 AM10/5/18
to rubyonra...@googlegroups.com
Sounds good!

Alberto Almagro

unread,
Oct 5, 2018, 12:20:26 PM10/5/18
to Ruby on Rails: Core
That would be great Tim!

Jake Niemiec

unread,
Oct 5, 2018, 3:34:48 PM10/5/18
to rubyonra...@googlegroups.com
This might also be useful in the sense that JavaScript Date() timestamps are based on a time value that is milliseconds since the unix epoch. (as opposed to seconds for server-side timestamps)

--
Reply all
Reply to author
Forward
0 new messages