activesupport 6.0.6.1 → 7.1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +865 -438
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +6 -6
  5. data/lib/active_support/actionable_error.rb +4 -2
  6. data/lib/active_support/array_inquirer.rb +4 -2
  7. data/lib/active_support/backtrace_cleaner.rb +30 -10
  8. data/lib/active_support/benchmarkable.rb +4 -3
  9. data/lib/active_support/broadcast_logger.rb +250 -0
  10. data/lib/active_support/builder.rb +1 -1
  11. data/lib/active_support/cache/coder.rb +153 -0
  12. data/lib/active_support/cache/entry.rb +134 -0
  13. data/lib/active_support/cache/file_store.rb +53 -20
  14. data/lib/active_support/cache/mem_cache_store.rb +208 -63
  15. data/lib/active_support/cache/memory_store.rb +120 -38
  16. data/lib/active_support/cache/null_store.rb +16 -2
  17. data/lib/active_support/cache/redis_cache_store.rb +201 -208
  18. data/lib/active_support/cache/serializer_with_fallback.rb +175 -0
  19. data/lib/active_support/cache/strategy/local_cache.rb +73 -66
  20. data/lib/active_support/cache.rb +539 -261
  21. data/lib/active_support/callbacks.rb +273 -142
  22. data/lib/active_support/code_generator.rb +65 -0
  23. data/lib/active_support/concern.rb +53 -7
  24. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +44 -7
  25. data/lib/active_support/concurrency/null_lock.rb +13 -0
  26. data/lib/active_support/concurrency/share_lock.rb +2 -2
  27. data/lib/active_support/configurable.rb +19 -6
  28. data/lib/active_support/configuration_file.rb +51 -0
  29. data/lib/active_support/core_ext/array/access.rb +1 -5
  30. data/lib/active_support/core_ext/array/conversions.rb +15 -13
  31. data/lib/active_support/core_ext/array/grouping.rb +6 -6
  32. data/lib/active_support/core_ext/array/inquiry.rb +2 -2
  33. data/lib/active_support/core_ext/benchmark.rb +2 -2
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +1 -1
  35. data/lib/active_support/core_ext/class/attribute.rb +34 -44
  36. data/lib/active_support/core_ext/class/subclasses.rb +19 -29
  37. data/lib/active_support/core_ext/date/blank.rb +1 -1
  38. data/lib/active_support/core_ext/date/calculations.rb +24 -9
  39. data/lib/active_support/core_ext/date/conversions.rb +18 -16
  40. data/lib/active_support/core_ext/date_and_time/calculations.rb +27 -4
  41. data/lib/active_support/core_ext/date_and_time/compatibility.rb +15 -0
  42. data/lib/active_support/core_ext/date_time/blank.rb +1 -1
  43. data/lib/active_support/core_ext/date_time/calculations.rb +4 -0
  44. data/lib/active_support/core_ext/date_time/conversions.rb +19 -15
  45. data/lib/active_support/core_ext/digest/uuid.rb +30 -13
  46. data/lib/active_support/core_ext/enumerable.rb +146 -72
  47. data/lib/active_support/core_ext/erb/util.rb +196 -0
  48. data/lib/active_support/core_ext/file/atomic.rb +3 -1
  49. data/lib/active_support/core_ext/hash/conversions.rb +3 -4
  50. data/lib/active_support/core_ext/hash/deep_merge.rb +22 -14
  51. data/lib/active_support/core_ext/hash/deep_transform_values.rb +4 -4
  52. data/lib/active_support/core_ext/hash/indifferent_access.rb +3 -3
  53. data/lib/active_support/core_ext/hash/keys.rb +5 -5
  54. data/lib/active_support/core_ext/hash/slice.rb +3 -2
  55. data/lib/active_support/core_ext/integer/inflections.rb +12 -12
  56. data/lib/active_support/core_ext/kernel/reporting.rb +4 -4
  57. data/lib/active_support/core_ext/kernel/singleton_class.rb +1 -1
  58. data/lib/active_support/core_ext/load_error.rb +1 -1
  59. data/lib/active_support/core_ext/module/attr_internal.rb +2 -2
  60. data/lib/active_support/core_ext/module/attribute_accessors.rb +31 -29
  61. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +51 -20
  62. data/lib/active_support/core_ext/module/concerning.rb +14 -8
  63. data/lib/active_support/core_ext/module/delegation.rb +75 -42
  64. data/lib/active_support/core_ext/module/deprecation.rb +15 -12
  65. data/lib/active_support/core_ext/module/introspection.rb +1 -26
  66. data/lib/active_support/core_ext/name_error.rb +23 -2
  67. data/lib/active_support/core_ext/numeric/bytes.rb +9 -0
  68. data/lib/active_support/core_ext/numeric/conversions.rb +82 -73
  69. data/lib/active_support/core_ext/object/acts_like.rb +29 -5
  70. data/lib/active_support/core_ext/object/blank.rb +2 -2
  71. data/lib/active_support/core_ext/object/deep_dup.rb +17 -1
  72. data/lib/active_support/core_ext/object/duplicable.rb +15 -4
  73. data/lib/active_support/core_ext/object/inclusion.rb +13 -5
  74. data/lib/active_support/core_ext/object/instance_variables.rb +22 -12
  75. data/lib/active_support/core_ext/object/json.rb +52 -28
  76. data/lib/active_support/core_ext/object/to_query.rb +2 -4
  77. data/lib/active_support/core_ext/object/try.rb +20 -20
  78. data/lib/active_support/core_ext/object/with.rb +44 -0
  79. data/lib/active_support/core_ext/object/with_options.rb +25 -6
  80. data/lib/active_support/core_ext/object.rb +1 -0
  81. data/lib/active_support/core_ext/pathname/blank.rb +16 -0
  82. data/lib/active_support/core_ext/pathname/existence.rb +23 -0
  83. data/lib/active_support/core_ext/pathname.rb +4 -0
  84. data/lib/active_support/core_ext/range/compare_range.rb +6 -25
  85. data/lib/active_support/core_ext/range/conversions.rb +34 -13
  86. data/lib/active_support/core_ext/range/each.rb +1 -1
  87. data/lib/active_support/core_ext/range/overlap.rb +40 -0
  88. data/lib/active_support/core_ext/range.rb +1 -2
  89. data/lib/active_support/core_ext/regexp.rb +8 -1
  90. data/lib/active_support/core_ext/securerandom.rb +25 -13
  91. data/lib/active_support/core_ext/string/access.rb +5 -24
  92. data/lib/active_support/core_ext/string/conversions.rb +3 -2
  93. data/lib/active_support/core_ext/string/filters.rb +21 -15
  94. data/lib/active_support/core_ext/string/indent.rb +1 -1
  95. data/lib/active_support/core_ext/string/inflections.rb +51 -10
  96. data/lib/active_support/core_ext/string/inquiry.rb +2 -1
  97. data/lib/active_support/core_ext/string/multibyte.rb +2 -2
  98. data/lib/active_support/core_ext/string/output_safety.rb +85 -194
  99. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -2
  100. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  101. data/lib/active_support/core_ext/symbol.rb +3 -0
  102. data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
  103. data/lib/active_support/core_ext/time/calculations.rb +46 -8
  104. data/lib/active_support/core_ext/time/conversions.rb +16 -13
  105. data/lib/active_support/core_ext/time/zones.rb +12 -28
  106. data/lib/active_support/core_ext.rb +2 -1
  107. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  108. data/lib/active_support/current_attributes.rb +54 -22
  109. data/lib/active_support/deep_mergeable.rb +53 -0
  110. data/lib/active_support/dependencies/autoload.rb +17 -12
  111. data/lib/active_support/dependencies/interlock.rb +10 -18
  112. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  113. data/lib/active_support/dependencies.rb +58 -769
  114. data/lib/active_support/deprecation/behaviors.rb +77 -38
  115. data/lib/active_support/deprecation/constant_accessor.rb +5 -4
  116. data/lib/active_support/deprecation/deprecators.rb +104 -0
  117. data/lib/active_support/deprecation/disallowed.rb +54 -0
  118. data/lib/active_support/deprecation/instance_delegator.rb +31 -5
  119. data/lib/active_support/deprecation/method_wrappers.rb +12 -28
  120. data/lib/active_support/deprecation/proxy_wrappers.rb +40 -25
  121. data/lib/active_support/deprecation/reporting.rb +76 -16
  122. data/lib/active_support/deprecation.rb +36 -4
  123. data/lib/active_support/deprecator.rb +7 -0
  124. data/lib/active_support/descendants_tracker.rb +150 -68
  125. data/lib/active_support/digest.rb +5 -3
  126. data/lib/active_support/duration/iso8601_parser.rb +3 -3
  127. data/lib/active_support/duration/iso8601_serializer.rb +24 -12
  128. data/lib/active_support/duration.rb +136 -56
  129. data/lib/active_support/encrypted_configuration.rb +72 -9
  130. data/lib/active_support/encrypted_file.rb +46 -13
  131. data/lib/active_support/environment_inquirer.rb +40 -0
  132. data/lib/active_support/error_reporter/test_helper.rb +15 -0
  133. data/lib/active_support/error_reporter.rb +203 -0
  134. data/lib/active_support/evented_file_update_checker.rb +86 -137
  135. data/lib/active_support/execution_context/test_helper.rb +13 -0
  136. data/lib/active_support/execution_context.rb +53 -0
  137. data/lib/active_support/execution_wrapper.rb +31 -12
  138. data/lib/active_support/executor/test_helper.rb +7 -0
  139. data/lib/active_support/file_update_checker.rb +4 -2
  140. data/lib/active_support/fork_tracker.rb +79 -0
  141. data/lib/active_support/gem_version.rb +5 -5
  142. data/lib/active_support/gzip.rb +2 -0
  143. data/lib/active_support/hash_with_indifferent_access.rb +86 -42
  144. data/lib/active_support/html_safe_translation.rb +53 -0
  145. data/lib/active_support/i18n.rb +2 -1
  146. data/lib/active_support/i18n_railtie.rb +29 -27
  147. data/lib/active_support/inflector/inflections.rb +26 -9
  148. data/lib/active_support/inflector/methods.rb +54 -64
  149. data/lib/active_support/inflector/transliterate.rb +7 -5
  150. data/lib/active_support/isolated_execution_state.rb +76 -0
  151. data/lib/active_support/json/decoding.rb +6 -5
  152. data/lib/active_support/json/encoding.rb +31 -45
  153. data/lib/active_support/key_generator.rb +32 -7
  154. data/lib/active_support/lazy_load_hooks.rb +33 -7
  155. data/lib/active_support/locale/en.yml +10 -4
  156. data/lib/active_support/log_subscriber/test_helper.rb +2 -2
  157. data/lib/active_support/log_subscriber.rb +101 -32
  158. data/lib/active_support/logger.rb +9 -60
  159. data/lib/active_support/logger_silence.rb +2 -26
  160. data/lib/active_support/logger_thread_safe_level.rb +24 -25
  161. data/lib/active_support/message_encryptor.rb +205 -58
  162. data/lib/active_support/message_encryptors.rb +141 -0
  163. data/lib/active_support/message_pack/cache_serializer.rb +23 -0
  164. data/lib/active_support/message_pack/extensions.rb +292 -0
  165. data/lib/active_support/message_pack/serializer.rb +63 -0
  166. data/lib/active_support/message_pack.rb +50 -0
  167. data/lib/active_support/message_verifier.rb +237 -86
  168. data/lib/active_support/message_verifiers.rb +135 -0
  169. data/lib/active_support/messages/codec.rb +65 -0
  170. data/lib/active_support/messages/metadata.rb +112 -46
  171. data/lib/active_support/messages/rotation_configuration.rb +2 -1
  172. data/lib/active_support/messages/rotation_coordinator.rb +93 -0
  173. data/lib/active_support/messages/rotator.rb +35 -32
  174. data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
  175. data/lib/active_support/multibyte/chars.rb +15 -52
  176. data/lib/active_support/multibyte/unicode.rb +8 -122
  177. data/lib/active_support/multibyte.rb +1 -1
  178. data/lib/active_support/notifications/fanout.rb +310 -105
  179. data/lib/active_support/notifications/instrumenter.rb +113 -48
  180. data/lib/active_support/notifications.rb +56 -29
  181. data/lib/active_support/number_helper/number_converter.rb +15 -8
  182. data/lib/active_support/number_helper/number_to_currency_converter.rb +11 -6
  183. data/lib/active_support/number_helper/number_to_delimited_converter.rb +1 -1
  184. data/lib/active_support/number_helper/number_to_human_converter.rb +1 -1
  185. data/lib/active_support/number_helper/number_to_human_size_converter.rb +5 -5
  186. data/lib/active_support/number_helper/number_to_phone_converter.rb +2 -1
  187. data/lib/active_support/number_helper/number_to_rounded_converter.rb +9 -5
  188. data/lib/active_support/number_helper/rounding_helper.rb +12 -32
  189. data/lib/active_support/number_helper.rb +379 -304
  190. data/lib/active_support/option_merger.rb +11 -18
  191. data/lib/active_support/ordered_hash.rb +4 -4
  192. data/lib/active_support/ordered_options.rb +23 -3
  193. data/lib/active_support/parameter_filter.rb +104 -75
  194. data/lib/active_support/proxy_object.rb +2 -0
  195. data/lib/active_support/rails.rb +1 -4
  196. data/lib/active_support/railtie.rb +90 -6
  197. data/lib/active_support/reloader.rb +12 -4
  198. data/lib/active_support/rescuable.rb +18 -16
  199. data/lib/active_support/ruby_features.rb +7 -0
  200. data/lib/active_support/secure_compare_rotator.rb +58 -0
  201. data/lib/active_support/security_utils.rb +19 -12
  202. data/lib/active_support/string_inquirer.rb +5 -3
  203. data/lib/active_support/subscriber.rb +23 -47
  204. data/lib/active_support/syntax_error_proxy.rb +70 -0
  205. data/lib/active_support/tagged_logging.rb +84 -23
  206. data/lib/active_support/test_case.rb +166 -27
  207. data/lib/active_support/testing/assertions.rb +73 -20
  208. data/lib/active_support/testing/autorun.rb +0 -2
  209. data/lib/active_support/testing/constant_stubbing.rb +32 -0
  210. data/lib/active_support/testing/deprecation.rb +53 -2
  211. data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
  212. data/lib/active_support/testing/isolation.rb +30 -29
  213. data/lib/active_support/testing/method_call_assertions.rb +24 -11
  214. data/lib/active_support/testing/parallelization/server.rb +82 -0
  215. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  216. data/lib/active_support/testing/parallelization.rb +16 -95
  217. data/lib/active_support/testing/parallelize_executor.rb +81 -0
  218. data/lib/active_support/testing/stream.rb +4 -6
  219. data/lib/active_support/testing/strict_warnings.rb +39 -0
  220. data/lib/active_support/testing/tagged_logging.rb +1 -1
  221. data/lib/active_support/testing/time_helpers.rb +89 -19
  222. data/lib/active_support/time_with_zone.rb +105 -70
  223. data/lib/active_support/values/time_zone.rb +59 -26
  224. data/lib/active_support/version.rb +1 -1
  225. data/lib/active_support/xml_mini/jdom.rb +4 -11
  226. data/lib/active_support/xml_mini/libxml.rb +5 -5
  227. data/lib/active_support/xml_mini/libxmlsax.rb +1 -1
  228. data/lib/active_support/xml_mini/nokogiri.rb +5 -5
  229. data/lib/active_support/xml_mini/nokogirisax.rb +2 -2
  230. data/lib/active_support/xml_mini/rexml.rb +9 -2
  231. data/lib/active_support/xml_mini.rb +7 -6
  232. data/lib/active_support.rb +40 -1
  233. metadata +127 -40
  234. data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -5
  235. data/lib/active_support/core_ext/hash/compact.rb +0 -5
  236. data/lib/active_support/core_ext/hash/transform_values.rb +0 -5
  237. data/lib/active_support/core_ext/marshal.rb +0 -24
  238. data/lib/active_support/core_ext/module/reachable.rb +0 -6
  239. data/lib/active_support/core_ext/numeric/inquiry.rb +0 -5
  240. data/lib/active_support/core_ext/range/include_range.rb +0 -9
  241. data/lib/active_support/core_ext/range/include_time_with_zone.rb +0 -23
  242. data/lib/active_support/core_ext/range/overlaps.rb +0 -10
  243. data/lib/active_support/core_ext/uri.rb +0 -25
  244. data/lib/active_support/dependencies/zeitwerk_integration.rb +0 -117
  245. data/lib/active_support/per_thread_registry.rb +0 -60
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/dependencies/autoload"
4
-
5
3
  module ActiveSupport
6
4
  module NumberHelper
7
5
  extend ActiveSupport::Autoload
@@ -20,361 +18,438 @@ module ActiveSupport
20
18
 
21
19
  extend self
22
20
 
23
- # Formats a +number+ into a phone number (US by default e.g., (555)
24
- # 123-9876). You can customize the format in the +options+ hash.
21
+ # Formats +number+ into a phone number.
25
22
  #
26
- # ==== Options
23
+ # number_to_phone(5551234) # => "555-1234"
24
+ # number_to_phone("5551234") # => "555-1234"
25
+ # number_to_phone(1235551234) # => "123-555-1234"
26
+ # number_to_phone("12x34") # => "12x34"
27
27
  #
28
- # * <tt>:area_code</tt> - Adds parentheses around the area code.
29
- # * <tt>:delimiter</tt> - Specifies the delimiter to use
30
- # (defaults to "-").
31
- # * <tt>:extension</tt> - Specifies an extension to add to the
32
- # end of the generated number.
33
- # * <tt>:country_code</tt> - Sets the country code for the phone
34
- # number.
35
- # * <tt>:pattern</tt> - Specifies how the number is divided into three
36
- # groups with the custom regexp to override the default format.
37
- # ==== Examples
38
- #
39
- # number_to_phone(5551234) # => "555-1234"
40
- # number_to_phone('5551234') # => "555-1234"
41
- # number_to_phone(1235551234) # => "123-555-1234"
42
- # number_to_phone(1235551234, area_code: true) # => "(123) 555-1234"
43
- # number_to_phone(1235551234, delimiter: ' ') # => "123 555 1234"
44
- # number_to_phone(1235551234, area_code: true, extension: 555) # => "(123) 555-1234 x 555"
45
- # number_to_phone(1235551234, country_code: 1) # => "+1-123-555-1234"
46
- # number_to_phone('123a456') # => "123a456"
47
- #
48
- # number_to_phone(1235551234, country_code: 1, extension: 1343, delimiter: '.')
28
+ # number_to_phone(1235551234, delimiter: ".", country_code: 1, extension: 1343)
49
29
  # # => "+1.123.555.1234 x 1343"
50
30
  #
51
- # number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
52
- # # => "(755) 6123-4567"
53
- # number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
54
- # # => "133-1234-5678"
31
+ # ==== Options
32
+ #
33
+ # [+:area_code+]
34
+ # Whether to use parentheses for the area code. Defaults to false.
35
+ #
36
+ # number_to_phone(1235551234, area_code: true)
37
+ # # => "(123) 555-1234"
38
+ #
39
+ # [+:delimiter+]
40
+ # The digit group delimiter to use. Defaults to <tt>"-"</tt>.
41
+ #
42
+ # number_to_phone(1235551234, delimiter: " ")
43
+ # # => "123 555 1234"
44
+ #
45
+ # [+:country_code+]
46
+ # A country code to prepend.
47
+ #
48
+ # number_to_phone(1235551234, country_code: 1)
49
+ # # => "+1-123-555-1234"
50
+ #
51
+ # [+:extension+]
52
+ # An extension to append.
53
+ #
54
+ # number_to_phone(1235551234, extension: 555)
55
+ # # => "123-555-1234 x 555"
56
+ #
57
+ # [+:pattern+]
58
+ # A regexp that specifies how the digits should be grouped. The first
59
+ # three captures from the regexp are treated as digit groups.
60
+ #
61
+ # number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
62
+ # # => "133-1234-5678"
63
+ # number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
64
+ # # => "(755) 6123-4567"
65
+ #
55
66
  def number_to_phone(number, options = {})
56
67
  NumberToPhoneConverter.convert(number, options)
57
68
  end
58
69
 
59
- # Formats a +number+ into a currency string (e.g., $13.65). You
60
- # can customize the format in the +options+ hash.
70
+ # Formats a +number+ into a currency string.
71
+ #
72
+ # number_to_currency(1234567890.50) # => "$1,234,567,890.50"
73
+ # number_to_currency(1234567890.506) # => "$1,234,567,890.51"
74
+ # number_to_currency("12x34") # => "$12x34"
75
+ #
76
+ # number_to_currency(1234567890.50, unit: "&pound;", separator: ",", delimiter: "")
77
+ # # => "&pound;1234567890,50"
61
78
  #
62
79
  # The currency unit and number formatting of the current locale will be used
63
- # unless otherwise specified in the provided options. No currency conversion
64
- # is performed. If the user is given a way to change their locale, they will
80
+ # unless otherwise specified via options. No currency conversion is
81
+ # performed. If the user is given a way to change their locale, they will
65
82
  # also be able to change the relative value of the currency displayed with
66
83
  # this helper. If your application will ever support multiple locales, you
67
- # may want to specify a constant <tt>:locale</tt> option or consider
68
- # using a library capable of currency conversion.
84
+ # may want to specify a constant +:locale+ option or consider using a
85
+ # library capable of currency conversion.
69
86
  #
70
87
  # ==== Options
71
88
  #
72
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
73
- # (defaults to current locale).
74
- # * <tt>:precision</tt> - Sets the level of precision (defaults
75
- # to 2).
76
- # * <tt>:unit</tt> - Sets the denomination of the currency
77
- # (defaults to "$").
78
- # * <tt>:separator</tt> - Sets the separator between the units
79
- # (defaults to ".").
80
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
81
- # to ",").
82
- # * <tt>:format</tt> - Sets the format for non-negative numbers
83
- # (defaults to "%u%n"). Fields are <tt>%u</tt> for the
84
- # currency, and <tt>%n</tt> for the number.
85
- # * <tt>:negative_format</tt> - Sets the format for negative
86
- # numbers (defaults to prepending a hyphen to the formatted
87
- # number given by <tt>:format</tt>). Accepts the same fields
88
- # than <tt>:format</tt>, except <tt>%n</tt> is here the
89
- # absolute value of the number.
90
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
91
- # insignificant zeros after the decimal separator (defaults to
92
- # +false+).
93
- #
94
- # ==== Examples
95
- #
96
- # number_to_currency(1234567890.50) # => "$1,234,567,890.50"
97
- # number_to_currency(1234567890.506) # => "$1,234,567,890.51"
98
- # number_to_currency(1234567890.506, precision: 3) # => "$1,234,567,890.506"
99
- # number_to_currency(1234567890.506, locale: :fr) # => "1 234 567 890,51 €"
100
- # number_to_currency('123a456') # => "$123a456"
101
- #
102
- # number_to_currency("123a456", raise: true) # => InvalidNumberError
103
- #
104
- # number_to_currency(-0.456789, precision: 0)
105
- # # => "$0"
106
- # number_to_currency(-1234567890.50, negative_format: '(%u%n)')
107
- # # => "($1,234,567,890.50)"
108
- # number_to_currency(1234567890.50, unit: '&pound;', separator: ',', delimiter: '')
109
- # # => "&pound;1234567890,50"
110
- # number_to_currency(1234567890.50, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
111
- # # => "1234567890,50 &pound;"
112
- # number_to_currency(1234567890.50, strip_insignificant_zeros: true)
113
- # # => "$1,234,567,890.5"
89
+ # [+:locale+]
90
+ # The locale to use for formatting. Defaults to the current locale.
91
+ #
92
+ # number_to_currency(1234567890.506, locale: :fr)
93
+ # # => "1 234 567 890,51 €"
94
+ #
95
+ # [+:precision+]
96
+ # The level of precision. Defaults to 2.
97
+ #
98
+ # number_to_currency(1234567890.123, precision: 3) # => "$1,234,567,890.123"
99
+ # number_to_currency(0.456789, precision: 0) # => "$0"
100
+ #
101
+ # [+:round_mode+]
102
+ # Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
103
+ # +:default+.
104
+ #
105
+ # number_to_currency(1234567890.01, precision: 0, round_mode: :up)
106
+ # # => "$1,234,567,891"
107
+ #
108
+ # [+:unit+]
109
+ # The denomination of the currency. Defaults to <tt>"$"</tt>.
110
+ #
111
+ # [+:separator+]
112
+ # The decimal separator. Defaults to <tt>"."</tt>.
113
+ #
114
+ # [+:delimiter+]
115
+ # The thousands delimiter. Defaults to <tt>","</tt>.
116
+ #
117
+ # [+:format+]
118
+ # The format for non-negative numbers. <tt>%u</tt> represents the currency,
119
+ # and <tt>%n</tt> represents the number. Defaults to <tt>"%u%n"</tt>.
120
+ #
121
+ # number_to_currency(1234567890.50, format: "%n %u")
122
+ # # => "1,234,567,890.50 $"
123
+ #
124
+ # [+:negative_format+]
125
+ # The format for negative numbers. <tt>%u</tt> and <tt>%n</tt> behave the
126
+ # same as in +:format+, but <tt>%n</tt> represents the absolute value of
127
+ # the number. Defaults to the value of +:format+ prepended with <tt>-</tt>.
128
+ #
129
+ # number_to_currency(-1234567890.50, negative_format: "(%u%n)")
130
+ # # => "($1,234,567,890.50)"
131
+ #
132
+ # [+:strip_insignificant_zeros+]
133
+ # Whether to remove insignificant zeros after the decimal separator.
134
+ # Defaults to false.
135
+ #
136
+ # number_to_currency(1234567890.50, strip_insignificant_zeros: true)
137
+ # # => "$1,234,567,890.5"
138
+ #
114
139
  def number_to_currency(number, options = {})
115
140
  NumberToCurrencyConverter.convert(number, options)
116
141
  end
117
142
 
118
- # Formats a +number+ as a percentage string (e.g., 65%). You can
119
- # customize the format in the +options+ hash.
143
+ # Formats +number+ as a percentage string.
144
+ #
145
+ # number_to_percentage(100) # => "100.000%"
146
+ # number_to_percentage("99") # => "99.000%"
147
+ # number_to_percentage("99x") # => "99x%"
148
+ #
149
+ # number_to_percentage(12345.6789, delimiter: ".", separator: ",", precision: 2)
150
+ # # => "12.345,68%"
120
151
  #
121
152
  # ==== Options
122
153
  #
123
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
124
- # (defaults to current locale).
125
- # * <tt>:precision</tt> - Sets the precision of the number
126
- # (defaults to 3). Keeps the number's precision if +nil+.
127
- # * <tt>:significant</tt> - If +true+, precision will be the number
128
- # of significant_digits. If +false+, the number of fractional
129
- # digits (defaults to +false+).
130
- # * <tt>:separator</tt> - Sets the separator between the
131
- # fractional and integer digits (defaults to ".").
132
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
133
- # to "").
134
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
135
- # insignificant zeros after the decimal separator (defaults to
136
- # +false+).
137
- # * <tt>:format</tt> - Specifies the format of the percentage
138
- # string The number field is <tt>%n</tt> (defaults to "%n%").
139
- #
140
- # ==== Examples
141
- #
142
- # number_to_percentage(100) # => "100.000%"
143
- # number_to_percentage('98') # => "98.000%"
144
- # number_to_percentage(100, precision: 0) # => "100%"
145
- # number_to_percentage(1000, delimiter: '.', separator: ',') # => "1.000,000%"
146
- # number_to_percentage(302.24398923423, precision: 5) # => "302.24399%"
147
- # number_to_percentage(1000, locale: :fr) # => "1000,000%"
148
- # number_to_percentage(1000, precision: nil) # => "1000%"
149
- # number_to_percentage('98a') # => "98a%"
150
- # number_to_percentage(100, format: '%n %') # => "100.000 %"
154
+ # [+:locale+]
155
+ # The locale to use for formatting. Defaults to the current locale.
156
+ #
157
+ # number_to_percentage(1000, locale: :fr)
158
+ # # => "1000,000%"
159
+ #
160
+ # [+:precision+]
161
+ # The level of precision, or +nil+ to preserve +number+'s precision.
162
+ # Defaults to 2.
163
+ #
164
+ # number_to_percentage(12.3456789, precision: 4) # => "12.3457%"
165
+ # number_to_percentage(99.999, precision: 0) # => "100%"
166
+ # number_to_percentage(99.999, precision: nil) # => "99.999%"
167
+ #
168
+ # [+:round_mode+]
169
+ # Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
170
+ # +:default+.
171
+ #
172
+ # number_to_percentage(12.3456789, precision: 4, round_mode: :down)
173
+ # # => "12.3456%"
174
+ #
175
+ # [+:significant+]
176
+ # Whether +:precision+ should be applied to significant digits instead of
177
+ # fractional digits. Defaults to false.
178
+ #
179
+ # number_to_percentage(12345.6789) # => "12345.679%"
180
+ # number_to_percentage(12345.6789, significant: true) # => "12300%"
181
+ # number_to_percentage(12345.6789, precision: 2) # => "12345.68%"
182
+ # number_to_percentage(12345.6789, precision: 2, significant: true) # => "12000%"
183
+ #
184
+ # [+:separator+]
185
+ # The decimal separator. Defaults to <tt>"."</tt>.
186
+ #
187
+ # [+:delimiter+]
188
+ # The thousands delimiter. Defaults to <tt>","</tt>.
189
+ #
190
+ # [+:strip_insignificant_zeros+]
191
+ # Whether to remove insignificant zeros after the decimal separator.
192
+ # Defaults to false.
193
+ #
194
+ # [+:format+]
195
+ # The format of the output. <tt>%n</tt> represents the number. Defaults to
196
+ # <tt>"%n%"</tt>.
197
+ #
198
+ # number_to_percentage(100, format: "%n %")
199
+ # # => "100.000 %"
200
+ #
151
201
  def number_to_percentage(number, options = {})
152
202
  NumberToPercentageConverter.convert(number, options)
153
203
  end
154
204
 
155
- # Formats a +number+ with grouped thousands using +delimiter+
156
- # (e.g., 12,324). You can customize the format in the +options+
157
- # hash.
205
+ # Formats +number+ by grouping thousands with a delimiter.
206
+ #
207
+ # number_to_delimited(12345678) # => "12,345,678"
208
+ # number_to_delimited("123456") # => "123,456"
209
+ # number_to_delimited(12345678.9876) # => "12,345,678.9876"
210
+ # number_to_delimited("12x34") # => "12x34"
211
+ #
212
+ # number_to_delimited(12345678.9876, delimiter: ".", separator: ",")
213
+ # # => "12.345.678,9876"
158
214
  #
159
215
  # ==== Options
160
216
  #
161
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
162
- # (defaults to current locale).
163
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
164
- # to ",").
165
- # * <tt>:separator</tt> - Sets the separator between the
166
- # fractional and integer digits (defaults to ".").
167
- # * <tt>:delimiter_pattern</tt> - Sets a custom regular expression used for
168
- # deriving the placement of delimiter. Helpful when using currency formats
169
- # like INR.
170
- #
171
- # ==== Examples
172
- #
173
- # number_to_delimited(12345678) # => "12,345,678"
174
- # number_to_delimited('123456') # => "123,456"
175
- # number_to_delimited(12345678.05) # => "12,345,678.05"
176
- # number_to_delimited(12345678, delimiter: '.') # => "12.345.678"
177
- # number_to_delimited(12345678, delimiter: ',') # => "12,345,678"
178
- # number_to_delimited(12345678.05, separator: ' ') # => "12,345,678 05"
179
- # number_to_delimited(12345678.05, locale: :fr) # => "12 345 678,05"
180
- # number_to_delimited('112a') # => "112a"
181
- # number_to_delimited(98765432.98, delimiter: ' ', separator: ',')
182
- # # => "98 765 432,98"
183
- # number_to_delimited("123456.78",
184
- # delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/)
185
- # # => "1,23,456.78"
217
+ # [+:locale+]
218
+ # The locale to use for formatting. Defaults to the current locale.
219
+ #
220
+ # number_to_delimited(12345678.05, locale: :fr)
221
+ # # => "12 345 678,05"
222
+ #
223
+ # [+:delimiter+]
224
+ # The thousands delimiter. Defaults to <tt>","</tt>.
225
+ #
226
+ # number_to_delimited(12345678, delimiter: ".")
227
+ # # => "12.345.678"
228
+ #
229
+ # [+:separator+]
230
+ # The decimal separator. Defaults to <tt>"."</tt>.
231
+ #
232
+ # number_to_delimited(12345678.05, separator: " ")
233
+ # # => "12,345,678 05"
234
+ #
235
+ # [+:delimiter_pattern+]
236
+ # A regexp to determine the placement of delimiters. Helpful when using
237
+ # currency formats like INR.
238
+ #
239
+ # number_to_delimited("123456.78", delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/)
240
+ # # => "1,23,456.78"
241
+ #
186
242
  def number_to_delimited(number, options = {})
187
243
  NumberToDelimitedConverter.convert(number, options)
188
244
  end
189
245
 
190
- # Formats a +number+ with the specified level of
191
- # <tt>:precision</tt> (e.g., 112.32 has a precision of 2 if
192
- # +:significant+ is +false+, and 5 if +:significant+ is +true+).
193
- # You can customize the format in the +options+ hash.
246
+ # Formats +number+ to a specific level of precision.
247
+ #
248
+ # number_to_rounded(12345.6789) # => "12345.679"
249
+ # number_to_rounded(12345.6789, precision: 2) # => "12345.68"
250
+ # number_to_rounded(12345.6789, precision: 0) # => "12345"
251
+ # number_to_rounded(12345, precision: 5) # => "12345.00000"
194
252
  #
195
253
  # ==== Options
196
254
  #
197
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
198
- # (defaults to current locale).
199
- # * <tt>:precision</tt> - Sets the precision of the number
200
- # (defaults to 3). Keeps the number's precision if +nil+.
201
- # * <tt>:significant</tt> - If +true+, precision will be the number
202
- # of significant_digits. If +false+, the number of fractional
203
- # digits (defaults to +false+).
204
- # * <tt>:separator</tt> - Sets the separator between the
205
- # fractional and integer digits (defaults to ".").
206
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
207
- # to "").
208
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
209
- # insignificant zeros after the decimal separator (defaults to
210
- # +false+).
211
- #
212
- # ==== Examples
213
- #
214
- # number_to_rounded(111.2345) # => "111.235"
215
- # number_to_rounded(111.2345, precision: 2) # => "111.23"
216
- # number_to_rounded(13, precision: 5) # => "13.00000"
217
- # number_to_rounded(389.32314, precision: 0) # => "389"
218
- # number_to_rounded(111.2345, significant: true) # => "111"
219
- # number_to_rounded(111.2345, precision: 1, significant: true) # => "100"
220
- # number_to_rounded(13, precision: 5, significant: true) # => "13.000"
221
- # number_to_rounded(13, precision: nil) # => "13"
222
- # number_to_rounded(111.234, locale: :fr) # => "111,234"
223
- #
224
- # number_to_rounded(13, precision: 5, significant: true, strip_insignificant_zeros: true)
225
- # # => "13"
226
- #
227
- # number_to_rounded(389.32314, precision: 4, significant: true) # => "389.3"
228
- # number_to_rounded(1111.2345, precision: 2, separator: ',', delimiter: '.')
229
- # # => "1.111,23"
255
+ # [+:locale+]
256
+ # The locale to use for formatting. Defaults to the current locale.
257
+ #
258
+ # number_to_rounded(111.234, locale: :fr)
259
+ # # => "111,234"
260
+ #
261
+ # [+:precision+]
262
+ # The level of precision, or +nil+ to preserve +number+'s precision.
263
+ # Defaults to 3.
264
+ #
265
+ # number_to_rounded(12345.6789, precision: nil)
266
+ # # => "12345.6789"
267
+ #
268
+ # [+:round_mode+]
269
+ # Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
270
+ # +:default+.
271
+ #
272
+ # number_to_rounded(12.34, precision: 0, round_mode: :up)
273
+ # # => "13"
274
+ #
275
+ # [+:significant+]
276
+ # Whether +:precision+ should be applied to significant digits instead of
277
+ # fractional digits. Defaults to false.
278
+ #
279
+ # number_to_rounded(12345.6789) # => "12345.679"
280
+ # number_to_rounded(12345.6789, significant: true) # => "12300"
281
+ # number_to_rounded(12345.6789, precision: 2) # => "12345.68"
282
+ # number_to_rounded(12345.6789, precision: 2, significant: true) # => "12000"
283
+ #
284
+ # [+:separator+]
285
+ # The decimal separator. Defaults to <tt>"."</tt>.
286
+ #
287
+ # [+:delimiter+]
288
+ # The thousands delimiter. Defaults to <tt>","</tt>.
289
+ #
290
+ # [+:strip_insignificant_zeros+]
291
+ # Whether to remove insignificant zeros after the decimal separator.
292
+ # Defaults to false.
293
+ #
294
+ # number_to_rounded(12.34, strip_insignificant_zeros: false) # => "12.340"
295
+ # number_to_rounded(12.34, strip_insignificant_zeros: true) # => "12.34"
296
+ # number_to_rounded(12.3456, strip_insignificant_zeros: true) # => "12.346"
297
+ #
230
298
  def number_to_rounded(number, options = {})
231
299
  NumberToRoundedConverter.convert(number, options)
232
300
  end
233
301
 
234
- # Formats the bytes in +number+ into a more understandable
235
- # representation (e.g., giving it 1500 yields 1.5 KB). This
236
- # method is useful for reporting file sizes to users. You can
237
- # customize the format in the +options+ hash.
302
+ # Formats +number+ as bytes into a more human-friendly representation.
303
+ # Useful for reporting file sizes to users.
304
+ #
305
+ # number_to_human_size(123) # => "123 Bytes"
306
+ # number_to_human_size(1234) # => "1.21 KB"
307
+ # number_to_human_size(12345) # => "12.1 KB"
308
+ # number_to_human_size(1234567) # => "1.18 MB"
309
+ # number_to_human_size(1234567890) # => "1.15 GB"
310
+ # number_to_human_size(1234567890123) # => "1.12 TB"
311
+ # number_to_human_size(1234567890123456) # => "1.1 PB"
312
+ # number_to_human_size(1234567890123456789) # => "1.07 EB"
238
313
  #
239
- # See <tt>number_to_human</tt> if you want to pretty-print a
240
- # generic number.
314
+ # See #number_to_human if you want to pretty-print a generic number.
241
315
  #
242
316
  # ==== Options
243
317
  #
244
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
245
- # (defaults to current locale).
246
- # * <tt>:precision</tt> - Sets the precision of the number
247
- # (defaults to 3).
248
- # * <tt>:significant</tt> - If +true+, precision will be the number
249
- # of significant_digits. If +false+, the number of fractional
250
- # digits (defaults to +true+)
251
- # * <tt>:separator</tt> - Sets the separator between the
252
- # fractional and integer digits (defaults to ".").
253
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
254
- # to "").
255
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
256
- # insignificant zeros after the decimal separator (defaults to
257
- # +true+)
258
- #
259
- # ==== Examples
260
- #
261
- # number_to_human_size(123) # => "123 Bytes"
262
- # number_to_human_size(1234) # => "1.21 KB"
263
- # number_to_human_size(12345) # => "12.1 KB"
264
- # number_to_human_size(1234567) # => "1.18 MB"
265
- # number_to_human_size(1234567890) # => "1.15 GB"
266
- # number_to_human_size(1234567890123) # => "1.12 TB"
267
- # number_to_human_size(1234567890123456) # => "1.1 PB"
268
- # number_to_human_size(1234567890123456789) # => "1.07 EB"
269
- # number_to_human_size(1234567, precision: 2) # => "1.2 MB"
270
- # number_to_human_size(483989, precision: 2) # => "470 KB"
271
- # number_to_human_size(1234567, precision: 2, separator: ',') # => "1,2 MB"
272
- # number_to_human_size(1234567890123, precision: 5) # => "1.1228 TB"
273
- # number_to_human_size(524288000, precision: 5) # => "500 MB"
318
+ # [+:locale+]
319
+ # The locale to use for formatting. Defaults to the current locale.
320
+ #
321
+ # [+:precision+]
322
+ # The level of precision. Defaults to 3.
323
+ #
324
+ # number_to_human_size(123456, precision: 2) # => "120 KB"
325
+ # number_to_human_size(1234567, precision: 2) # => "1.2 MB"
326
+ #
327
+ # [+:round_mode+]
328
+ # Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
329
+ # +:default+.
330
+ #
331
+ # number_to_human_size(123456, precision: 2, round_mode: :up)
332
+ # # => "130 KB"
333
+ #
334
+ # [+:significant+]
335
+ # Whether +:precision+ should be applied to significant digits instead of
336
+ # fractional digits. Defaults to true.
337
+ #
338
+ # [+:separator+]
339
+ # The decimal separator. Defaults to <tt>"."</tt>.
340
+ #
341
+ # number_to_human_size(1234567, separator: ",")
342
+ # # => "1,18 MB"
343
+ #
344
+ # [+:delimiter+]
345
+ # The thousands delimiter. Defaults to <tt>","</tt>.
346
+ #
347
+ # [+:strip_insignificant_zeros+]
348
+ # Whether to remove insignificant zeros after the decimal separator.
349
+ # Defaults to true.
350
+ #
274
351
  def number_to_human_size(number, options = {})
275
352
  NumberToHumanSizeConverter.convert(number, options)
276
353
  end
277
354
 
278
- # Pretty prints (formats and approximates) a number in a way it
279
- # is more readable by humans (eg.: 1200000000 becomes "1.2
280
- # Billion"). This is useful for numbers that can get very large
281
- # (and too hard to read).
355
+ # Formats +number+ into a more human-friendly representation. Useful for
356
+ # numbers that can become very large and too hard to read.
282
357
  #
283
- # See <tt>number_to_human_size</tt> if you want to print a file
284
- # size.
358
+ # number_to_human(123) # => "123"
359
+ # number_to_human(1234) # => "1.23 Thousand"
360
+ # number_to_human(12345) # => "12.3 Thousand"
361
+ # number_to_human(1234567) # => "1.23 Million"
362
+ # number_to_human(1234567890) # => "1.23 Billion"
363
+ # number_to_human(1234567890123) # => "1.23 Trillion"
364
+ # number_to_human(1234567890123456) # => "1.23 Quadrillion"
365
+ # number_to_human(1234567890123456789) # => "1230 Quadrillion"
285
366
  #
286
- # You can also define your own unit-quantifier names if you want
287
- # to use other decimal units (eg.: 1500 becomes "1.5
288
- # kilometers", 0.150 becomes "150 milliliters", etc). You may
289
- # define a wide range of unit quantifiers, even fractional ones
290
- # (centi, deci, mili, etc).
367
+ # See #number_to_human_size if you want to pretty-print a file size.
291
368
  #
292
369
  # ==== Options
293
370
  #
294
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
295
- # (defaults to current locale).
296
- # * <tt>:precision</tt> - Sets the precision of the number
297
- # (defaults to 3).
298
- # * <tt>:significant</tt> - If +true+, precision will be the number
299
- # of significant_digits. If +false+, the number of fractional
300
- # digits (defaults to +true+)
301
- # * <tt>:separator</tt> - Sets the separator between the
302
- # fractional and integer digits (defaults to ".").
303
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
304
- # to "").
305
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
306
- # insignificant zeros after the decimal separator (defaults to
307
- # +true+)
308
- # * <tt>:units</tt> - A Hash of unit quantifier names. Or a
309
- # string containing an i18n scope where to find this hash. It
310
- # might have the following keys:
311
- # * *integers*: <tt>:unit</tt>, <tt>:ten</tt>,
312
- # <tt>:hundred</tt>, <tt>:thousand</tt>, <tt>:million</tt>,
313
- # <tt>:billion</tt>, <tt>:trillion</tt>,
314
- # <tt>:quadrillion</tt>
315
- # * *fractionals*: <tt>:deci</tt>, <tt>:centi</tt>,
316
- # <tt>:mili</tt>, <tt>:micro</tt>, <tt>:nano</tt>,
317
- # <tt>:pico</tt>, <tt>:femto</tt>
318
- # * <tt>:format</tt> - Sets the format of the output string
319
- # (defaults to "%n %u"). The field types are:
320
- # * %u - The quantifier (ex.: 'thousand')
321
- # * %n - The number
322
- #
323
- # ==== Examples
324
- #
325
- # number_to_human(123) # => "123"
326
- # number_to_human(1234) # => "1.23 Thousand"
327
- # number_to_human(12345) # => "12.3 Thousand"
328
- # number_to_human(1234567) # => "1.23 Million"
329
- # number_to_human(1234567890) # => "1.23 Billion"
330
- # number_to_human(1234567890123) # => "1.23 Trillion"
331
- # number_to_human(1234567890123456) # => "1.23 Quadrillion"
332
- # number_to_human(1234567890123456789) # => "1230 Quadrillion"
333
- # number_to_human(489939, precision: 2) # => "490 Thousand"
334
- # number_to_human(489939, precision: 4) # => "489.9 Thousand"
335
- # number_to_human(1234567, precision: 4,
336
- # significant: false) # => "1.2346 Million"
337
- # number_to_human(1234567, precision: 1,
338
- # separator: ',',
339
- # significant: false) # => "1,2 Million"
340
- #
341
- # number_to_human(500000000, precision: 5) # => "500 Million"
342
- # number_to_human(12345012345, significant: false) # => "12.345 Billion"
343
- #
344
- # Non-significant zeros after the decimal separator are stripped
345
- # out by default (set <tt>:strip_insignificant_zeros</tt> to
346
- # +false+ to change that):
347
- #
348
- # number_to_human(12.00001) # => "12"
349
- # number_to_human(12.00001, strip_insignificant_zeros: false) # => "12.0"
350
- #
351
- # ==== Custom Unit Quantifiers
352
- #
353
- # You can also use your own custom unit quantifiers:
354
- # number_to_human(500000, units: { unit: 'ml', thousand: 'lt' }) # => "500 lt"
355
- #
356
- # If in your I18n locale you have:
357
- #
358
- # distance:
359
- # centi:
360
- # one: "centimeter"
361
- # other: "centimeters"
362
- # unit:
363
- # one: "meter"
364
- # other: "meters"
365
- # thousand:
366
- # one: "kilometer"
367
- # other: "kilometers"
368
- # billion: "gazillion-distance"
369
- #
370
- # Then you could do:
371
- #
372
- # number_to_human(543934, units: :distance) # => "544 kilometers"
373
- # number_to_human(54393498, units: :distance) # => "54400 kilometers"
374
- # number_to_human(54393498000, units: :distance) # => "54.4 gazillion-distance"
375
- # number_to_human(343, units: :distance, precision: 1) # => "300 meters"
376
- # number_to_human(1, units: :distance) # => "1 meter"
377
- # number_to_human(0.34, units: :distance) # => "34 centimeters"
371
+ # [+:locale+]
372
+ # The locale to use for formatting. Defaults to the current locale.
373
+ #
374
+ # [+:precision+]
375
+ # The level of precision. Defaults to 3.
376
+ #
377
+ # number_to_human(123456, precision: 2) # => "120 Thousand"
378
+ # number_to_human(123456, precision: 4) # => "123.5 Thousand"
379
+ #
380
+ # [+:round_mode+]
381
+ # Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
382
+ # +:default+.
383
+ #
384
+ # number_to_human(123456, precision: 2, round_mode: :up)
385
+ # # => "130 Thousand"
386
+ #
387
+ # [+:significant+]
388
+ # Whether +:precision+ should be applied to significant digits instead of
389
+ # fractional digits. Defaults to true.
390
+ #
391
+ # [+:separator+]
392
+ # The decimal separator. Defaults to <tt>"."</tt>.
393
+ #
394
+ # number_to_human(123456, precision: 4, separator: ",")
395
+ # # => "123,5 Thousand"
396
+ #
397
+ # [+:delimiter+]
398
+ # The thousands delimiter. Defaults to <tt>","</tt>.
399
+ #
400
+ # [+:strip_insignificant_zeros+]
401
+ # Whether to remove insignificant zeros after the decimal separator.
402
+ # Defaults to true.
403
+ #
404
+ # number_to_human(1000000) # => "1 Million"
405
+ # number_to_human(1000000, strip_insignificant_zeros: false) # => "1.00 Million"
406
+ # number_to_human(10.01) # => "10"
407
+ # number_to_human(10.01, strip_insignificant_zeros: false) # => "10.0"
408
+ #
409
+ # [+:format+]
410
+ # The format of the output. <tt>%n</tt> represents the number, and
411
+ # <tt>%u</tt> represents the quantifier (e.g., "Thousand"). Defaults to
412
+ # <tt>"%n %u"</tt>.
413
+ #
414
+ # [+:units+]
415
+ # A Hash of custom unit quantifier names.
416
+ #
417
+ # number_to_human(1, units: { unit: "m", thousand: "km" }) # => "1 m"
418
+ # number_to_human(100, units: { unit: "m", thousand: "km" }) # => "100 m"
419
+ # number_to_human(1000, units: { unit: "m", thousand: "km" }) # => "1 km"
420
+ # number_to_human(100000, units: { unit: "m", thousand: "km" }) # => "100 km"
421
+ # number_to_human(10000000, units: { unit: "m", thousand: "km" }) # => "10000 km"
422
+ #
423
+ # The following keys are supported for integer units: +:unit+, +:ten+,
424
+ # +:hundred+, +:thousand+, +:million+, +:billion+, +:trillion+,
425
+ # +:quadrillion+. Additionally, the following keys are supported for
426
+ # fractional units: +:deci+, +:centi+, +:mili+, +:micro+, +:nano+,
427
+ # +:pico+, +:femto+.
428
+ #
429
+ # The Hash can also be defined as a scope in an I18n locale. For example:
430
+ #
431
+ # en:
432
+ # distance:
433
+ # centi:
434
+ # one: "centimeter"
435
+ # other: "centimeters"
436
+ # unit:
437
+ # one: "meter"
438
+ # other: "meters"
439
+ # thousand:
440
+ # one: "kilometer"
441
+ # other: "kilometers"
442
+ #
443
+ # Then it can be specified by name:
444
+ #
445
+ # number_to_human(1, units: :distance) # => "1 meter"
446
+ # number_to_human(100, units: :distance) # => "100 meters"
447
+ # number_to_human(1000, units: :distance) # => "1 kilometer"
448
+ # number_to_human(100000, units: :distance) # => "100 kilometers"
449
+ # number_to_human(10000000, units: :distance) # => "10000 kilometers"
450
+ # number_to_human(0.1, units: :distance) # => "10 centimeters"
451
+ # number_to_human(0.01, units: :distance) # => "1 centimeter"
452
+ #
378
453
  def number_to_human(number, options = {})
379
454
  NumberToHumanConverter.convert(number, options)
380
455
  end