Skip to content

Instantly share code, notes, and snippets.

Created June 7, 2013 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e3b3d9aac9b21c2b7ed1 to your computer and use it in GitHub Desktop.
Save anonymous/e3b3d9aac9b21c2b7ed1 to your computer and use it in GitHub Desktop.
number = 1234567890
num_string = number.to_s
num_array = []
work_array = []
num_string.each_char {|c| num_array.push(c)}
for i in 0..4
work_array.push(num_array.shift.to_i)
end
work_array.each {|i| puts "#{i} is #{work_array[i].class}"}
@genericlady
Copy link

no reason to convert integer to string, just wrap it in quotes

num_string = "1234567890"

using the .split method will convert a string of characters into an array

num_array = num_string.split("")

use map to convert the array of strings into integers and store in a new variable

work_array = num_array.map { |num| num.to_i}

work_array.each { |i| puts "#{i} is #{i.class}"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment