Skip to content

Commit 4232495

Browse files
author
David Heinemeier Hansson
committed
Added Numeric#in_milliseconds, like 1.hour.in_milliseconds, so we can feed them to JavaScript functions like getTime().
1 parent db41eb8 commit 4232495

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

activesupport/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Added Numeric#in_milliseconds, like 1.hour.in_milliseconds, so we can feed them to JavaScript functions like getTime().
2+
3+
*DHH*
4+
15
* Calling ActiveSupport::JSON.decode with unsupported options now raises an error.
26

37
*Godfrey Chan*

activesupport/lib/active_support/core_ext/numeric/time.rb

+6
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,10 @@ def since(time = ::Time.current)
7676

7777
# Reads best without arguments: 10.minutes.from_now
7878
alias :from_now :since
79+
80+
# Used with the standard time durations, like 1.hour.in_milliseconds --
81+
# so we can feed them to JavaScript functions like getTime().
82+
def in_milliseconds
83+
self * 1000
84+
end
7985
end

activesupport/test/core_ext/numeric_ext_test.rb

+4
Original file line numberDiff line numberDiff line change
@@ -440,4 +440,8 @@ def test_to_s__injected_on_proper_types
440440
assert_equal BigDecimal, BigDecimal("1000010").class
441441
assert_equal '1 Million', BigDecimal("1000010").to_s(:human)
442442
end
443+
444+
def test_in_milliseconds
445+
assert_equal 10_000, 10.seconds.in_milliseconds
446+
end
443447
end

0 commit comments

Comments
 (0)