Project

General

Profile

Actions

Feature #12447

closed

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

Added by mrkn (Kenta Murata) about 9 years ago. Updated about 9 years ago.

Status:
Closed
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 3 (1 open2 closed)

Related to Ruby - Feature #4787: Integer#each_modulo(n)Closedmatz (Yukihiro Matsumoto)Actions
Related to Ruby - Feature #12116: `Fixnum#divmod`, `Bignum#divmod` with multiple argumentsOpenActions
Related to Ruby - Feature #16011: Digit groupingRejectedmatz (Yukihiro Matsumoto)Actions
Actions #1

Updated by mrkn (Kenta Murata) about 9 years ago

Actions #2

Updated by mrkn (Kenta Murata) about 9 years ago

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

Updated by mrkn (Kenta Murata) about 9 years ago

  • Description updated (diff)

Updated by mrkn (Kenta Murata) about 9 years ago

  • Description updated (diff)

Updated by matz (Yukihiro Matsumoto) about 9 years ago

Approved.

Matz.

Updated by akr (Akira Tanaka) about 9 years 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.

Updated by mrkn (Kenta Murata) about 9 years 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.
Actions #8

Updated by mrkn (Kenta Murata) about 9 years ago

  • Status changed from Open to Closed

Applied in changeset r55395.


numeric.c: Add Integer#digits [Feature #12447] [ruby-core:75799]

Actions #9

Updated by shyouhei (Shyouhei Urabe) about 6 years ago

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy