Project

General

Profile

Feature #12447

Updated by mrkn (Kenta Murata) 10 months ago

I sometimes want to extract digits of place-value notation of integer numbers.
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://github.com/mrkn/ruby/commit/6072e8a53bc67c40969b7749ed9d7f02ec0eb458

Back