Problem
You need to iterate over every character in a string.
Solution
Use the each_byte string iterator:
aString.each_byte { |c| # do something with c }
Discussion
Strings are enumerable. This means that you have a number of predefined methods for enumerating or iterating over each element of the collection. each_byte is a specialization of the String#each which allows you to specify your separator so you can iterate over characters, words, sentances or whatever.
Contrast
- perl: foreach (split //, $str) { … }
- python: TODO
- c: int max = strlen(s); for (int i=0; i<max; ++i) { char c = s[i]; … }
Related
- String#each
- Enumerable
Status: In Progress