Skip to content

Give to_base an array for an array, with no limit on the base - #350

Open
trans wants to merge 1 commit into
mainfrom
feature/to-base-arrays
Open

trans wants to merge 1 commit into
mainfrom
feature/to-base-arrays

Conversation

@trans

@trans trans commented Nov 18, 2026

Copy link
Copy Markdown
Member

The result of Integer#to_base now takes the form of the base it is given.

256.to_base(256)              #=> "\x01\x00"        a number: a string, as before
37.to_base("OLTWFS")          #=> "LOL"             a string of digits: a string, as before
1000.to_base([100])           #=> [10, 0]           an array holding the base: digit values
37.to_base(%w[O L T W F S])   #=> ["L", "O", "L"]   an array of digits: an array of them
5.to_base([:zero, :one])      #=> [:one, :zero, :one]
  • A plain number as the base still stops at 62 (and 256), where the digit characters run out. The array form has no limit.
  • An array of digits used to give a string, so the digits had to be single characters. They can now be any objects; .join gives the string.
  • Array#from_base takes the same arrays, so n.to_base(b).from_base(b) is n for any array b.
  • An array can't show a sign, so a negative number raises ArgumentError.
  • 1000.to_base(100) still raises, and the message now points to to_base([100]).

String#from_base is unchanged, and still accepts an array of single characters as digits.

None of this has been released, so nothing changes for anyone on 4.1.0.

🤖 Generated with Claude Code

Integer#to_base took an array of single characters as digits and gave
a string, and a base above 62 had no form at all except 256. Now the
result takes the form of the base: given an array, it gives an array.

- An array holding one Integer is a base of any size, and the result
  is the digit values: 1000.to_base([100]) is [10, 0].
- Any other array is the digits, which can be any objects:
  37.to_base(%w[O L T W F S]) is ["L", "O", "L"], where it was
  "LOL".

Array#from_base takes the same arrays, so it reads back what to_base
gave.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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