[ruby-core:75980] [Ruby trunk Feature#12447] Integer#digits for extracting digits of place-value notation in any base
From:
akr@...
Date:
2016-06-13 08:05:14 UTC
List:
ruby-core #75980
Issue #12447 has been updated by Akira Tanaka.
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.
----------------------------------------
Feature #12447: Integer#digits for extracting digits of place-value notation in any base
https://bugs.ruby-lang.org/issues/12447#change-59174
* Author: Kenta Murata
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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)`.
- https://github.com/joeljunstrom/ruby_luhn/blob/master/lib/luhn.rb#L43
- https://github.com/elitwin/check_digit/blob/master/lib/check_digit/damm.rb#L28
- https://github.com/elitwin/check_digit/blob/master/lib/check_digit/luhn.rb#L14
- https://github.com/elitwin/check_digit/blob/master/lib/check_digit/verhoeff.rb#L42
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
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>