From: Nathan Clark Date: 2010-12-29T08:07:34+09:00 Subject: Re: convert String "1;2;3;4;5;" to Array [1, 2, 3, 4, 5] > I'm trying to convert a String of numbers that are separated by > semicolons to an Array---totally for fun, to stretch my ruby > understanding, fyi. > > I use the Array in a while loop which does work when the Array looks > like = [1,2,3,4,5,...]---so that part is working. But I want to use ruby > to > convert a String = "1;2;3;4;5;6;7;8;9;10" into an Array [1,2,3,4,5,...] > so > that I can use these values. > > I've tried many a method, but can't seem to get the desired result; I've > tried gsub(/\;/, ","), eval (), and others. > > ########## > > raw_data = "1;2;3;4;5;6;7;8;9;10" > data = raw_data.split(/\;/) #but this gives ["1", "2", "3", "4", "5", > "6", "7", "8", "9", "10"], not [1, 2, 3,...] This is the right track, you just need to then convert each string to a number, e.g. raw_data.split(";").map { |raw| raw.to_i }