C "strtok" idiom in Ruby
In C, "strtok" destructively change a string into a series of strings:
char str[] = "begin space tab\t\tcarrige-return\ncomma, period. end"; char delim[] = " \t\n,."; for (char* p = strtok(str,delim); p && printf("%s\n", p); p = strtok(0, delim)) ;
Using a char pointer, we can output the tokens:
begin spaces tabs carriage-return comma period end
In Ruby, String#split with regex does the same and intuitively easy job:
"begin spaces tabs carriage-return comma, period. end".split(/[,.\s]+/).each {|tok| puts tok}