Q:

Write a Ruby program to split a delimited string into an array

0

Write a Ruby program to split a delimited string into an array. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

color = "Red, Green, Blue, White"
nums = "1, 3, 4, 5, 7"
print "Original delimited string:\n"
print color,", "
print nums
print "\nString to array:\n"
color_array = color.split(",")
nums_array = nums.split(",").map { |s| s.to_i }
print color_array
print "\n",nums_array

Output:

Original delimited string:
Red, Green, Blue, White1, 3, 4, 5, 7String to array:
["Red", " Green", " Blue", " White"]
[1, 3, 4, 5, 7]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now