Is there a method in ruby to turn fixnum like 74239
into an array like [7,4,2,3,9]
?
As of Ruby 2.4, integers (FixNum
is gone in 2.4+) have a built-in digits
method that extracts them into an array of their digits:
74239.digits
=> [9, 3, 2, 4, 7]
If you want to maintain the order of the digits, just chain reverse
:
74239.digits.reverse
=> [7, 4, 2, 3, 9]
Docs: https://ruby-doc.org/core-2.4.0/Integer.html#method-i-digits
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With