Readme.md in activesupport-duration-human_string-0.1.0 vs Readme.md in activesupport-duration-human_string-0.1.1

- old
+ new

@@ -1,12 +1,23 @@ -# `ActiveSupport::Duration#human_string` +# ActiveSupport::Duration#human_string [![Gem Version](https://badge.fury.io/rb/activesupport-duration-human_string.svg)](https://badge.fury.io/rb/activesupport-duration-human_string) -Convert [ActiveSupport::Duration](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html) objects to human-friendly strings like `'2h 30m 17s'`. +Convert [ActiveSupport::Duration](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html) +objects to human-friendly strings like `'2h 30m 17s'` or `'3y 6m 4d 12h 30m 5s'`. -- Like [`distance_of_time_in_words`](https://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html) helper but _exact_ rather than approximate. -- Like [`#inspect`](https://github.com/rails/rails/blob/b9ca94caea2ca6a6cc09abaffaad67b447134079/activesupport/lib/active_support/duration.rb#L372) but more concise and configurable. -- Like [`#iso8601`](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-iso8601) but more human readable rather than machine readable. +- Like [`distance_of_time_in_words`](https://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words) + helper but _exact_ rather than approximate + - `distance_of_time_in_words` is somewhat configurable but only gives an approximation (`'over 3 + years'`), with no option to print the exact number of each units. +- Like [`#inspect`](https://github.com/rails/rails/blob/b9ca94caea2ca6a6cc09abaffaad67b447134079/activesupport/lib/active_support/duration.rb#L372) + but more concise and configurable. + - `#inspect` is long (`'3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds'`) and not + at all configurable. +- Like [`#iso8601`](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-iso8601) + but more human readable rather than machine readable. + - `#iso8601` is concise and machine-readable, but not very human-readable (`'P3Y6M4DT12H30M5S'`)! +- Unlike [`#to_s`](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-to_s), + which is very concise (`'110839937'`) but not at all human-readable for large durations ## Installation Add this line to your application's Gemfile: @@ -15,18 +26,21 @@ ``` ## Usage ```ruby - duration = ActiveSupport::Duration.build(65) - duration.human_str # => '1m 5s' - duration.human_str(use_2_digit_numbers: true) # => '1m 05s' - - duration = ActiveSupport::Duration.build(3500) + duration = 3500.seconds duration.human_str # => '58m 20s' duration.human_str(delimiter: '') # => '58m20s' duration.human_str(separator: ' ') # => '58 m 20 s' duration.human_str(delimiter: ', ', separator: ' ') # => '58 m, 20 s' + + duration = ActiveSupport::Duration.parse "P3Y6M4DT12H30M5S" + # => 3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds + + duration.human_str # => "3y 6m 4d 12h 30m 5s" + (duration - 4.days).human_str # => "3y 6m 12h 30m 5s" + duration.human_str(delimiter: ', ') # => "3y, 6m, 4d, 12h, 30m, 5s" ``` Also aliased as `human_string`, `to_human_s`. ## Development