Project

General

Profile

Feature #12447

Integer#digits for extracting digits of place-value notation in any base

Added by mrkn (Kenta Murata) 12 months ago. Updated 12 months ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:75799]

Description

I sometimes want to extract digits of place-value notation of integer numbers.

For example:

12345.digits      #=> [5, 4, 3, 2, 1]
12345.digits(100) #=> [45, 23, 1]

One typical use case is calculating checksum digit.
I found some code extracting digits by the way like int.to_s.chars.map(&:to_i).

Introducing Integer#digits can reduce needless string allocation in such cases.

The patch was pushed into the repository in github.
https://github.com/mrkn/ruby/commit/5dce32d


Related issues

Related to Ruby trunk - Feature #4787: Integer#each_modulo(n) Assigned
Related to Ruby trunk - Feature #12116: `Fixnum#divmod`, `Bignum#divmod` with multiple arguments Open

Associated revisions

Revision 55395
Added by mrkn (Kenta Murata) 12 months ago

numeric.c: Add Integer#digits [Feature #12447]

Revision 55395
Added by mrkn (Kenta Murata) 12 months ago

numeric.c: Add Integer#digits [Feature #12447]

History

#1 Updated by mrkn (Kenta Murata) 12 months ago

#2 Updated by mrkn (Kenta Murata) 12 months ago

  • Related to Feature #12116: `Fixnum#divmod`, `Bignum#divmod` with multiple arguments added

#3 [ruby-core:75800] Updated by mrkn (Kenta Murata) 12 months ago

  • Description updated (diff)

#4 [ruby-core:75976] Updated by mrkn (Kenta Murata) 12 months ago

  • Description updated (diff)

#5 [ruby-core:75979] Updated by matz (Yukihiro Matsumoto) 12 months ago

Approved.

Matz.

#6 [ruby-core:75980] Updated by akr (Akira Tanaka) 12 months ago

I wanted such method to encode integer in some binary format.
(I cannot remember concrete example.)
For example, pack("w") can be implementd using Integer#digits.

#7 [ruby-core:75986] Updated by mrkn (Kenta Murata) 12 months ago

Summary of decision in today's Developer meeting:

  • The results endian is little endian.
  • Raise Math::DomainError for negative receiver.
  • Return [0] for 0.

#8 Updated by mrkn (Kenta Murata) 12 months ago

  • Status changed from Open to Closed

Applied in changeset r55395.


numeric.c: Add Integer#digits [Feature #12447]

Also available in: Atom PDF