How to split digits of an integer into an array of digits in Ruby

2 Answers

0 votes
n = 837

arr = n.to_s.split('').map { |digit| digit.to_i }

print arr



 
 
#
# run:
# 
# [8, 3, 7]
#

 



answered Oct 17, 2021 by avibootz
0 votes
n = 837
   
arr = n.digits.reverse()
 
print arr
   
   
   
    
    
#
# run:
# 
# [8, 3, 7]
#

 



answered Oct 18, 2021 by avibootz
...