https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112019-05-27T10:10:25ZRuby Issue Tracking SystemRuby master - Feature #15879: Proposal: Time#to_i accepts :unit keywordhttps://redmine.ruby-lang.org/issues/15879?journal_id=782402019-05-27T10:10:25Zsawa (Tsuyoshi Sawada)
<ul></ul><p>By analogy from <code>Time#round</code>, which takes as an argument an integer representing the precision in decimal places, and from <code>String#to_i</code>, which takes an integer, I think that passing an integer representing the number of decimal places would fit better. Something like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">{</span>
<span class="ss">event_id: </span><span class="nb">id</span><span class="p">,</span>
<span class="ss">name: </span><span class="nb">name</span><span class="p">,</span>
<span class="ss">tracked_at: </span><span class="n">tracked_at</span><span class="p">.</span><span class="nf">to_i</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span>
<span class="ss">tracked_at_micro: </span><span class="n">tracked_at</span><span class="p">.</span><span class="nf">to_i</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
<p>Default would be <code>0</code>.</p> Ruby master - Feature #15879: Proposal: Time#to_i accepts :unit keywordhttps://redmine.ruby-lang.org/issues/15879?journal_id=782412019-05-27T10:17:17Zsawa (Tsuyoshi Sawada)
<ul></ul><p>With <code>Float#to_i</code>, the decimal part is truncated, not rounded:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mf">123.7</span><span class="p">.</span><span class="nf">to_i</span> <span class="c1"># => 123</span>
<span class="mf">123.7</span><span class="p">.</span><span class="nf">round</span> <span class="c1"># => 124</span>
</code></pre>
<p>In contrast to this, your proposal with <code>Time#to_i</code> seems to return rounded results even though the method name is <code>to_i</code>. I think this is confusing. Is this your intention?</p> Ruby master - Feature #15879: Proposal: Time#to_i accepts :unit keywordhttps://redmine.ruby-lang.org/issues/15879?journal_id=782422019-05-27T10:25:26Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>You can also use <code>Time#to_r</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">{</span>
<span class="ss">event_id: </span><span class="nb">id</span><span class="p">,</span>
<span class="ss">name: </span><span class="nb">name</span><span class="p">,</span>
<span class="ss">tracked_at: </span><span class="p">(</span><span class="n">tracked_at</span><span class="p">.</span><span class="nf">to_r</span> <span class="o">*</span> <span class="mi">1_000</span><span class="p">).</span><span class="nf">to_i</span><span class="p">,</span>
<span class="ss">tracked_at_micro: </span><span class="p">(</span><span class="n">tracked_at</span><span class="p">.</span><span class="nf">to_r</span> <span class="o">*</span> <span class="mi">1_000_000</span><span class="p">).</span><span class="nf">to_i</span>
<span class="p">}</span>
</code></pre>
<p>At least it's always the same multiply-and-truncate operation.</p> Ruby master - Feature #15879: Proposal: Time#to_i accepts :unit keywordhttps://redmine.ruby-lang.org/issues/15879?journal_id=782432019-05-27T10:50:16Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I agree with sawa's comment - this is a bit surprising, at the least to me as well. It would<br>
be better to re-use the same idioms, so that ruby users are not surprised by behaviour, if<br>
it can be avoided.</p>
<p>But I think this is only one part of the suggestion; the other is about a simpler API,<br>
at the least that is how I understood it in the context of the example here.</p>
<p>I have no particular pro/con opinion per se on the proposed functionality; I have had<br>
to deal with time stamps in audio/video code too, so I understand the requirement to<br>
work with milliseconds, including making this data available to other languages or in<br>
e. g. json or yaml - but in general, ruby may have to be a bit conservative in what is<br>
exposed to other "downstream" ruby users API-wise. For example, some time ago <code>:exception</code><br>
added to some methods, which I think was a good/useful change, at the least for those<br>
ruby users who may require the functionality; ruby may have to be somewhat conservative<br>
in what may be exposed API-wise. It may be difficult to memorize 1000 different<br>
symbols. :D</p>
<p>Perhaps in the long run we could have something similar to <code>require 'English'</code>, defining some<br>
useful "decorator" symbols, such as in the example given above, via <code>unit: :milli</code>, and then<br>
"decorating" methods with this, when they meaningfully use time-related code - but I think<br>
that this may require some more discussion in general. Matz also made comments about this<br>
in the past in another context; I don't fully recall the idea, but I think it was somewhat<br>
related to "interfaces" to methods or method signatures or something like that.</p>
<p>I wanted to write a bit more, e. g. giving the example of ruby-gtk using more symbols,<br>
such as <code>:bold</code> rather than harder-to-remember <code>Pango::FontDescription::BOLD</code>, or something<br>
like that, but I think I already wrote too much, so I will stop here.</p> Ruby master - Feature #15879: Proposal: Time#to_i accepts :unit keywordhttps://redmine.ruby-lang.org/issues/15879?journal_id=782492019-05-28T05:28:11Zjoker1007 (Tomohiro Hashidate)kakyoin.hierophant@gmail.com
<ul></ul><blockquote>
<p>In contrast to this, your proposal with Time#to_i seems to return rounded results even though the method name is to_i. I think this is confusing. Is this your intention?</p>
</blockquote>
<p>It is not important whether the result is rounded or truncated. I'm sorry, this examples is not good.<br>
The main problem that I must write <code>1_000_000</code> whenever I need time as microseconds. And so, Such a situaion is often.</p> Ruby master - Feature #15879: Proposal: Time#to_i accepts :unit keywordhttps://redmine.ruby-lang.org/issues/15879?journal_id=801672019-07-29T07:32:01Zko1 (Koichi Sasada)
<ul></ul><p>joker-san, if you are interest about this ticket yet, could you file on our dev-meeting agenda?<br>
<a href="https://bugs.ruby-lang.org/issues/15996" class="external">https://bugs.ruby-lang.org/issues/15996</a></p>
<p>Thanks.</p>