# Converting number to its digits as an array

I'm new to ruby and I was trying to solve a problem where I convert a
number into an array in such a way.

If I have the number 12345, how do I take that number and put it into an
array but where each element is a digit of the number 12345?

If this is the wrong place to have posted such a question (for various
reasons) then let me know.

···

--
Posted via http://www.ruby-forum.com/.

Not terribly difficult to accomplish. Check out the .split() method of
the string class:

http://ruby-doc.org/core/classes/String.html#M000803

···

On Thu, 2010-09-16 at 18:17 -0500, Ian Ian wrote:

I'm new to ruby and I was trying to solve a problem where I convert a
number into an array in such a way.

If I have the number 12345, how do I take that number and put it into an
array but where each element is a digit of the number 12345?

If this is the wrong place to have posted such a question (for various
reasons) then let me know.

Victor qwerty wrote:

I'm new to ruby and I was trying to solve a problem where I convert a
number into an array in such a way.

If I have the number 12345, how do I take that number and put it into an
array but where each element is a digit of the number 12345?

If this is the wrong place to have posted such a question (for various
reasons) then let me know.

irb(main):001:0> "12345".scan(/./)
=> ["1", "2", "3", "4", "5"]

···

--
Posted via http://www.ruby-forum.com/\.

Ian Ian wrote:

I'm new to ruby and I was trying to solve a problem where I convert a
number into an array in such a way.

If I have the number 12345, how do I take that number and put it into an
array but where each element is a digit of the number 12345?

There's a myriad ways to do that. I find this one the simplest,
easiest to read and easiest to understand:

12345.to_s.chars.map(&:to_i)

jwm

Thank you.

···

--
Posted via http://www.ruby-forum.com/.

If I try one of these methods, I still can't go from element to element
in the array. I don't want to just show a list, I want an actual array.

···

--
Posted via http://www.ruby-forum.com/.

All of these methods return an Array. Why don't you show us what you're
trying to do.

···

On Sat, Sep 18, 2010 at 2:47 PM, Victor qwerty <ianphorsman@gmail.com>wrote:

If I try one of these methods, I still can't go from element to element
in the array. I don't want to just show a list, I want an actual array.
--
Posted via http://www.ruby-forum.com/\.

Josh Cheek wrote:

If I try one of these methods, I still can't go from element to element
in the array. I don't want to just show a list, I want an actual array.
--
Posted via http://www.ruby-forum.com/\.

All of these methods return an Array. Why don't you show us what you're
trying to do.

It says I can't do the each method, but if I have the array, can't I
iterate through each element in the array?

x = 5 ** 500

x.to_s.scan(/./)

y = 0

x.each do |i|

i.to_i

y += i

end

puts y

···

On Sat, Sep 18, 2010 at 2:47 PM, Victor qwerty > <ianphorsman@gmail.com>wrote:

--
Posted via http://www.ruby-forum.com/\.

Victor qwerty wrote:

x = 5 ** 500

x.to_s.scan(/./)

The value of this expression is an array, but you don't assign it to
anything, so it is lost. Try:

digits = x.to_s.scan(/./)

y = 0

x.each do |i|

digits.each do |i|

i.to_i

That again does nothing - it converts i to an integer, but the resulting

y += i

You probably meant:

val = i.to_i
y += val

or more simply,

y += i.to_i

···

end

puts y

--
Posted via http://www.ruby-forum.com/\.

Thank you so much! That really clarifies things. It did the program
successfully and now I know a little bit more about why.

···

--
Posted via http://www.ruby-forum.com/.