This post originated from an RSS feed registered with Ruby Buzz
by Jay Fields.
Original Post: Rails: String#each_char
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
Yields a single-character string for each character in the string. When $KCODE = ‘UTF8’, multi-byte characters are yielded appropriately.
Usage The String#each_char method is nice for iterating through a string, one character at a time. I generally use regex for string manipulation; however, when context within the string matters, each_char is helpful.
unit_tests do test "each_char can be used to strip every other charater"do strip, result =true,"" "hello world".each_char do |char| result << char if strip strip =!strip end assert_equal "hlowrd", result end end