Array item to integer

Hello,

I am stuck on a really dumb thing!

I simply want to get an item from an array and convert it to an integer.

For example:
“cube_column_numbers = [[“1”], [“2”], [“3”]]”

n=0
cube_column_numbers.each do |num|
x = (cube_column_numbers[n]).to_i
puts x

Just want to convert this to a list of integers…

Thanks,
Dave C., MD

hi, Here you go

cube_column_numbers = [[“1”], [“2”], [“3”]]

cube_column_numbers.each do |i|
i.each do |num|
puts num.to_i
end
end

Raja gopalan wrote in post #1171145:

hi, Here you go

cube_column_numbers = [[“1”], [“2”], [“3”]]

cube_column_numbers.each do |i|
i.each do |num|
puts num.to_i
end
end

Thank you Raja!

To convert that to a list(#Array) of integers you could do something
like this:

cube_column_numbers = [[“1”], [“2”], [“3”]]
cube_column_numbers.flatten!.map! {|i| i.to_i}

Have fun learning Ruby.

Damián M. González wrote in post #1171917:

To convert that to a list(#Array) of integers you could do something
like this:

cube_column_numbers = [[“1”], [“2”], [“3”]]
cube_column_numbers.flatten!.map! {|i| i.to_i}

Have fun learning Ruby.

Another way:

cube_column_numbers.map! {|x| x[0].to_i}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs