Reason #108 • April 18th, 2026

ActiveSupport: Numeric#megabytes and friends

Writing 5 * 1024 * 1024 is not the end of the world. But it is definitely the beginning of needless squinting. ActiveSupport gives us named byte helpers instead:

Ruby
5.megabytes
# => 5242880

1.gigabyte + 512.megabytes
# => 1610612736

256.kilobytes
# => 262144

# Also useful for intuitive conversions:
number_in_megabytes = 108.megabytes
number_of_megabytes = number_in_megabytes / 1.megabyte
# => 108
      
JavaScript
5 * 1024 * 1024;
// => 5242880

1 * 1024 * 1024 * 1024 + 512 * 1024 * 1024;
// => 1610612736

256 * 1024;
// => 262144

// Not very intuitive for conversions:
const numberInMegabytes = 108 * 1024 * 1024;
const numberOfMegabytes = numberInMegabytes / (1024 * 1024);
// => 108
      

The byte helpers go all the way from bytes to zettabytes.

Worth noting: these are binary units, based on 1024, not decimal 1000.

History

The byte helpers were already present when ActiveSupport was spun out into its own gem in version 0.10.0, released in 2005.

Reason #109 ?