Skip to content

Add Numeric#strfbytes - #354

Merged
trans merged 1 commit into
mainfrom
feature/strfbytes
Nov 28, 2026
Merged

trans merged 1 commit into
mainfrom
feature/strfbytes

Conversation

@trans

@trans trans commented Nov 28, 2026

Copy link
Copy Markdown
Member

Formats a number of bytes in the largest unit that fits. From the RichUnits gem.

512.strfbytes            #=> "512 bytes"
3000000.strfbytes        #=> "2.86 MB"
1536.strfbytes('%.0f')   #=> "2 KB"

It is a core method, loaded with require 'facets' or require 'facets/numeric/strfbytes'. The units are powers of 1024.

Changed from the gem's version:

  • The units go on past TB, up to YB. Before, anything of 1024 TB or more was shown as a count of bytes.
  • One byte is "1 byte".
  • A number that rounds up to 1024 moves to the next unit: one byte short of a megabyte is "1.00 MB", where it was "1024.00 KB".
  • Negative numbers are handled.

The gem's strfbits and the octet_units alias are not brought over.

8 tests; the whole suite passes.

🤖 Generated with Claude Code

From the RichUnits gem. It now goes past TB, up to YB, says "1 byte",
moves to the next unit when the number rounds up to 1024, and handles
negative numbers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@trans
trans merged commit 180c9c3 into main Nov 28, 2026
16 checks passed
@trans
trans deleted the feature/strfbytes branch November 29, 2026 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant