OneBite.Dev - Coding blog in a bite size

split a string by comma sign in Ruby

Code snippet on how to split a string by comma sign in Ruby

str.split(",")

This code uses the Ruby split method, which divides a string into separate pieces. In this example, the string is “Name, Age, Address” and the split method is used to separate it by the comma sign. This produces an array containing the three pieces: [“Name”, ” Age”, ” Address”]. The str variable is what is being split into three parts, so it is the one calling the split method. The comma sign is the parameter of the split method, which indicates what character should be used to separate the string. The split method returns the array of the resulting pieces.

ruby