From: shevegen@... Date: 2019-05-27T10:50:19+00:00 Subject: [ruby-core:92862] [Ruby trunk Feature#15879] Proposal: Time#to_i accepts :unit keyword Issue #15879 has been updated by shevegen (Robert A. Heiler). I agree with sawa's comment - this is a bit surprising, at the least to me as well. It would be better to re-use the same idioms, so that ruby users are not surprised by behaviour, if it can be avoided. But I think this is only one part of the suggestion; the other is about a simpler API, at the least that is how I understood it in the context of the example here. I have no particular pro/con opinion per se on the proposed functionality; I have had to deal with time stamps in audio/video code too, so I understand the requirement to work with milliseconds, including making this data available to other languages or in e. g. json or yaml - but in general, ruby may have to be a bit conservative in what is exposed to other "downstream" ruby users API-wise. For example, some time ago :exception added to some methods, which I think was a good/useful change, at the least for those ruby users who may require the functionality; ruby may have to be somewhat conservative in what may be exposed API-wise. It may be difficult to memorize 1000 different symbols. :D Perhaps in the long run we could have something similar to require 'English', defining some useful "decorator" symbols, such as in the example given above, via unit: :milli, and then "decorating" methods with this, when they meaningfully use time-related code - but I think that this may require some more discussion in general. Matz also made comments about this in the past in another context; I don't fully recall the idea, but I think it was somewhat related to "interfaces" to methods or method signatures or something like that. I wanted to write a bit more, e. g. giving the example of ruby-gtk using more symbols, such as :bold rather than harder-to-remember Pango::FontDescription::BOLD, or something like that, but I think I already wrote too much, so I will stop here. ---------------------------------------- Feature #15879: Proposal: Time#to_i accepts :unit keyword https://bugs.ruby-lang.org/issues/15879#change-78243 * Author: joker1007 (Tomohiro Hashidate) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I often need Unix time as microseconds or nanoseconds to serialize for other language environments. For example, Java uses milliseconds(nanoseconds) basically. In such a situation, current Ruby code is like below. ``` ruby { event_id: id, name: name, tracked_at: (tracked_at.to_f * 1000).round, tracked_at_micro: (tracked_at * 1000000 + tracked_at.usec) } ``` But this example is noisy. And it is easy to make a mistake. I want to write like below. ```ruby { event_id: id, name: name, tracked_at: tracked_at.to_i(unit: :milli), tracked_at_micro: tracked_at.to_i(unit: :micro) } # or { event_id: id, name: name, tracked_at: tracked_at.as_msec, tracked_at_micro: tracked_at.as_usec } ``` -- https://bugs.ruby-lang.org/ Unsubscribe: